0

I'm getting two errors with the following code. I'm essentially trying to go through all the custom taxonomies and add them to the post class.

Notice: Array to string conversion in /Applications/MAMP/htdocs/Dev/wp-includes/taxonomy.php on line 3317

Notice: Array to string conversion in
/Applications/MAMP/htdocs/Dev/wp-includes/category-template.php on
line 1253

if ( ! function_exists('custom_taxonomy_class') ) {

    function custom_taxonomy_class($classes, $class, $ID) {

       $args = array(
         'public'   => true,
         '_builtin' => false

       ); 
       $output = 'names'; // or objects
       $operator = 'and'; // 'and' or 'or'

       $taxonomies = get_taxonomies( $args, $output, $operator ); 
       $terms = get_the_terms( (int) $ID, (array)$taxonomies );

        if ( ! empty( $terms ) ) {
            foreach ( (array) $terms as $order => $term ) {            
                if ( ! in_array( $term->slug, $classes ) ) {
                    $classes[] = $term->slug;
                }
            }
        }

       $classes[] = '';

        return $classes;
    }
}

add_filter( 'post_class', 'custom_taxonomy_class', 10, 3 );
0

As a quick fix I looped through each custom taxonomy type.

if ( ! function_exists('custom_taxonomy_class') ) {

    function custom_taxonomy_class($classes, $class, $ID) {

       $args = array(
         'public'   => true,
         '_builtin' => false

       ); 
       $output = 'names'; // or objects
       $operator = 'and'; // 'and' or 'or'
       $taxonomies = get_taxonomies( $args, $output, $operator ); 
       foreach ($taxonomies as $key) {

        $terms = get_the_terms( (int) $ID, $key );

                if ( ! empty( $terms ) ) {
                    foreach ( (array) $terms as $order => $term ) {            
                        if ( ! in_array( $term->slug, $classes ) ) {
                            $classes[] = $term->slug;
                        }
                    }
                }
       }
       $classes[] = '';
       return $classes;
    }
}
add_filter( 'post_class', 'custom_taxonomy_class', 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.