0

I am new to php. I have these errors appearing on some Wordpress pages.

Warning: count(): Parameter must be an array or an object that implements Countable in /www/tastingvictoria_289/public/wp-content/themes/astra-child/template-parts/content-single.php on line 38

Warning: Invalid argument supplied for foreach() in /www/tastingvictoria_289/public/wp-content/themes/astra-child/template-parts/content-single.php on line 40

This is the related code.

<?php $terms = get_the_terms( $post->ID , 'category' ); 
        $total = count($terms); // 38
        $i=0;
        foreach ( $terms as $term ) {
            if($term->slug != "featured-post"){
                $i++;
                $term_link = get_term_link( $term, 'category' );
                if( is_wp_error( $term_link ) )
                continue;
                echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
                if ($i != $total) echo ' ';
            }
            
        } 
        ?>

Any explanation?

Lili
  • 333
  • 4
  • 23
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – cabrerahector Jul 19 '22 at 03:09

4 Answers4

0

As the error message, the $term parameter that you pass into count() function is not countable (in some case - eg, the post id is not exit).

To fix this, please change your code into:

<?php $terms = get_the_terms( $post->ID , 'category' );
if(is_array($terms)){
    $total = count($terms); // 38
    $i=0;
    foreach ( $terms as $term ) {
        if($term->slug != "featured-post"){
            $i++;
            $term_link = get_term_link( $term, 'category' );
            if( is_wp_error( $term_link ) )
            continue;
            echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
            if ($i != $total) echo ' ';
        }
        
    }
  }
?>
thống nguyễn
  • 765
  • 1
  • 5
  • 13
0

Convert the $terms to an array for getting valid result of count($terms).

ramadoiranedar
  • 137
  • 2
  • 7
0

get_the_terms() returns Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.

May be the terms not exists. Try following:

$terms = get_the_terms( $post->ID , 'category' );

if ( $terms && ! is_wp_error( $terms ) ) : 
$total = count($terms);
$i=0;
foreach ( $terms as $term ) {
    if($term->slug != "featured-post"){
        $i++;
        $term_link = get_term_link( $term, 'category' );
        if( is_wp_error( $term_link ) )
        continue;
        echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
        if ($i != $total) echo ' ';
    }
    
}
endif;
0

Mainly the get_the_terms() function behind code like this

function get_the_terms( $post, $taxonomy ) {
    $post = get_post( $post );
    if ( ! $post ) {
        return false;
    }
 
    $terms = get_object_term_cache( $post->ID, $taxonomy );
    if ( false === $terms ) {
        $terms = wp_get_object_terms( $post->ID, $taxonomy );
        if ( ! is_wp_error( $terms ) ) {
            $term_ids = wp_list_pluck( $terms, 'term_id' );
            wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
        }
    }
 
    /**
     * Filters the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param WP_Term[]|WP_Error $terms    Array of attached terms, or WP_Error on failure.
     * @param int                $post_id  Post ID.
     * @param string             $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
 
    if ( empty( $terms ) ) {
        return false;
    }
 
    return $terms;
}

In this case, you can see if there is no post then it'll return false, and if the function returns false then your code will be like this:

<?php $terms = get_the_terms( $post->ID , 'category' ); 
        $total = count($terms = false); // if there is no post
        $i=0;
        foreach ( false as $term ) { //if there is no post
            if($term->slug != "featured-post"){
                $i++;
                $term_link = get_term_link( $term, 'category' );
                if( is_wp_error( $term_link ) )
                continue;
                echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
                if ($i != $total) echo ' ';
            }
            
        } 
   ?>

Note: the count() function accept an array or object and the foreach() function accept iterable_expression as well. That is why you're getting the warnings.

So, in that case, you can check the return output of the get_the_terms() function like this:

<?php $terms = get_the_terms( $post->ID , 'category' );
if(is_iterable($terms)){
    $total = count($terms); // 38
    $i=0;
    foreach ( $terms as $term ) {
        if($term->slug != "featured-post"){
            $i++;
            $term_link = get_term_link( $term, 'category' );
            if( is_wp_error( $term_link ) )
            continue;
            echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
            if ($i != $total) echo ' ';
        }
        
        }
      }else{
        //do something
      }
    ?>

Thank you

thesharifdev
  • 184
  • 5