admin管理员组文章数量:1334133
I added custom image and icon to my custom taxonomy, and I know how to display it in the front end in the taxonomy template page.
$headImageId = get_term_meta( get_queried_object_id(), 'category-image', true );
$headImageUrl = ( ( $headImageId != '' ) ? wp_get_attachment_url( $headImageId ) : '' );
But how do I call it in my single post template? I want to use the image from the taxonomy term the custom post belongs to as a header background. I can't get my head around this..
I added custom image and icon to my custom taxonomy, and I know how to display it in the front end in the taxonomy template page.
$headImageId = get_term_meta( get_queried_object_id(), 'category-image', true );
$headImageUrl = ( ( $headImageId != '' ) ? wp_get_attachment_url( $headImageId ) : '' );
But how do I call it in my single post template? I want to use the image from the taxonomy term the custom post belongs to as a header background. I can't get my head around this..
Share Improve this question asked Jun 11, 2020 at 1:17 artist learning to codeartist learning to code 3311 gold badge5 silver badges18 bronze badges1 Answer
Reset to default 1You will need to use get_the_terms()
to get the taxonomy terms associated with the post and loop through them until you find one that has an image set:
$post. = get_queried_object();
$terms. = get_the_terms( $post, 'taxonomy_name_here' );
$image_url = null;
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$attachment_id = get_term_meta( $term->term_id, 'category-image', true );
if ( $attachment_id ) {
$image_url = wp_get_attachment_image_url( $attachment_id, 'full' );
break;
}
}
}
if ( $image_url ) {
// Output image as needed.
}
本文标签: How to gettermmeta on single custom post
版权声明:本文标题:How to get_term_meta on single custom post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742358528a2459898.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论