Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1331640
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI added a custom field to category using ACF, something like this:
so that I can specify a value for each category (like an image or text). How can I call for or display this or these values inside every post with the specified category. How do I write the code inside single.php?
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI added a custom field to category using ACF, something like this:
so that I can specify a value for each category (like an image or text). How can I call for or display this or these values inside every post with the specified category. How do I write the code inside single.php?
Share Improve this question asked Jul 13, 2020 at 1:13 L. coreL. core 3910 bronze badges 2- Check the ACF's documentation. – Sally CJ Commented Jul 13, 2020 at 6:26
- @Sally CJ Sorry, I couldn't find anything that can help here. – L. core Commented Jul 13, 2020 at 7:02
1 Answer
Reset to default 1You can use get_the_category()
to get the categories assigned to a specific post and use the_field()
or get_field()
to display/get the value of a specific ACF field. (You can also use get_term_meta()
in place of get_field()
)
So for example, the following will display a simple list of the categories (and I really mean plain simple), with the custom Image and Color fields next after the category name. Note that this assumes your Image field was set to return an image attachment's ID.
$cats = get_the_category();
if ( ! empty( $cats ) ) {
echo '<ul>';
foreach ( $cats as $term ) {
echo '<li>';
echo $term->name;
$selector = 'category_' . $term->term_id;
// Display the category image.
$att_id = get_field( 'image', $selector );
// $att_id = get_term_meta( $term->term_id, 'image', true );
echo wp_get_attachment_image( $att_id );
// Display the category color.
the_field( 'color', $selector );
// echo get_term_meta( $term->term_id, 'color', true );
echo '</li>';
}
echo '</ul>';
}
版权声明:本文标题:categories - How to display a value inside a post with a specified category from a category custom field? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742267822a2443740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论