admin管理员组文章数量:1391924
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
1 Answer
Reset to default -1Change operator to:
if($term->term_id === 4 ):
Check this for more: https://stackoverflow/a/80649/12785113
本文标签: termsConditional output based on taxonomy ID of custom post
版权声明:本文标题:terms - Conditional output based on taxonomy ID of custom post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744755872a2623458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论