terms - Conditional output based on taxonomy ID of custom post

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

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;
}

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;
}
Share Improve this question edited Feb 12, 2020 at 10:30 Patryk 694 bronze badges asked Feb 11, 2020 at 14:40 NielsPilonNielsPilon 3165 silver badges17 bronze badges 2
  • developer.wordpress/reference/functions/get_the_terms gets all terms of the post (which could be several); even if you have only one term, you still need to check the first array element. – Michael Commented Feb 11, 2020 at 23:18
  • That was the issue indeed, thanks! I've updated the question with the correct code. – NielsPilon Commented Feb 12, 2020 at 8:02
Add a comment  | 

1 Answer 1

Reset to default -1

Change operator to:

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744755872a4591877.html

相关推荐

  • terms - Conditional output based on taxonomy ID of custom post

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

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信