0

I'm working on a conditional statement to either output only the post thumbnail or a post thumbnail and a link for a custom post.

The output is based on a check if the post uses a specific taxonomy ID.

Getting the taxonomy ID of the post isn't an issue, however creating the conditional statement is.

This is the current code:

<?php $term = get_the_terms( $post_object->ID, 'my_taxonomy_name' );
// This if statement has to be incorrect
 if($term->term_id == 4 ):?>

<a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_post_thumbnail($post_object->ID, 'full'); ?></a>
// It always returns the following                          
<?php else:?>

<?php echo get_the_post_thumbnail($post_object->ID, 'full'); ?>

<?php endif;?>

This is the output of var_dump($term);

array(1) { [0]=> object(WP_Term)#10440 (10) { ["term_id"]=> int(4) ["name"]=> 
string(4) "Case" ["slug"]=> string(4) "case" ["term_group"]=> int(0) 
["term_taxonomy_id"]=> int(4) ["taxonomy"]=> string(16) "soort_referentie" 
["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(2) 
["filter"]=> string(3) "raw" } }

Any pointers to help me solve this issue are much appreciated.

Updated code with the solution:

$terms = get_the_terms( $post_object->ID, 'my_taxonomy_name' );

foreach($terms as $term){;                                                                                      

    if($term->term_id === 4 ):?>

    <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_post_thumbnail($post_object->ID, 'full'); ?></a>

    <?php else:

    echo get_the_post_thumbnail($post_object->ID, 'full');
    break;                          
    endif;
}
-1

Change operator to:

 if($term->term_id === 4 ):

Check this for more: https://stackoverflow.com/a/80649/12785113

| improve this answer | |
  • Thanks but unfortunately I'm only still getting only the output of the else statement for all posts which isn't correct. – NielsPilon Feb 11 at 15:17

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.