Conclusion taxonomy using foreach?

<?php $cur_terms = get_the_terms( $post--->ID, 'metki' );
foreach( $cur_terms as $cur_term ){
 $term_link = get_term_link( (int)$cur_term->term_id, $cur_term->taxonomy ) ;

 // If there was an error, continue to the next term.
 if ( is_wp_error( $term_link ) ) {
continue;
}
 echo '<a href="'. esc_url( $term_link ) .'">'. $cur_term->name .'</a> ';
};?>

In General, it taxonomies displays as you need, but if post has no tags, it throws the error "Invalid argument supplied for foreach() in line 52 - here it foreach( $cur_terms as $cur_term )"
How to fix can...
July 2nd 19 at 17:16
2 answers
July 2nd 19 at 17:18
Solution
To access the documentation for the function get_the_terms()to find the phrase "WP_Term Array of objects on success, false if there are no terms or the post does not exist, WP_Error on failure." and add a check:
<?php
$cur_terms = get_the_terms($post--->ID, 'metki');
if (is_array($cur_terms) && !is_wp_error($cur_terms)) {
 foreach ($cur_terms as $cur_term) {
 $term_link = get_term_link((int) $cur_term->term_id, $cur_term->taxonomy);

 // If there was an error, continue to the next term.
 if (is_wp_error($term_link)) {
continue;
}
 echo '<a href="' . esc_url($term_link) . '">' . $cur_term->name . '</a> ';
}
}
?>
sorry, I don't know PHP, so I was looking at where you're saying, but to lead him to such a he could not.
Right I understand that after we declared the array we check it exists and nessesity any error of VP. If 2 position return true, then there goes my cycle? - Timmy.Lockman80 commented on July 2nd 19 at 17:21
: so I just gave You the finished version of the code. It can be simply inserted, replacing your own :) I haven't tested it but it should work.

About how it works. Yes, we check that the returned function get_the_terms value is an array and not an instance of the WP_Error class. If everything is correct, then do a loop. In the case that will not be attached to the record of terms or the record itself would not exist, the function get_the_terms returns false, our condition is not fulfilled and the cycle will not start. - alexandrea_Vandervo commented on July 2nd 19 at 17:24
I know, I immediately inserted and checked, just explained why I didn't, although reading the documentation) - Timmy.Lockman80 commented on July 2nd 19 at 17:27
: :) Now I looked again at the code, I probably overdid it with the second condition "!is_wp_error($cur_terms)" and I think you can do without it. If the function get_the_terms returns an object of class WP_Error, he still will not validate is_array($cur_terms), so it should be enough :). - alexandrea_Vandervo commented on July 2nd 19 at 17:30
July 2nd 19 at 17:20
I think something like this:
if (count($cur_terms)) {
 foreach( $cur_terms as $cur_term )
}


in General, you need to check that the variable $cur_terms at the moment when an error occurs, and add a check.
I don't know php at all.
but I see the algorithm in your head, if (count($cur_terms)) is equal to zero, continue; to skip the foreach. How to implement, I don't know. And I don't know if so...
print_r($cur_terms); it is necessary where-that to add to see what is the error? - Timmy.Lockman80 commented on July 2nd 19 at 17:23
>> print_r($cur_terms); it is necessary where-that to add to see what is the error?
Yes, it is possible to add just above the foreach, then we will see not only error, but the contents of the object that foreach gets in and causes an error, respectively. - alexandrea_Vandervo commented on July 2nd 19 at 17:26
tried, it does not output anything. - Timmy.Lockman80 commented on July 2nd 19 at 17:29

Find more questions by tags WordPressWeb DevelopmentPHP