最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How to display a value inside a post with a specified category from a category custom field?

programmeradmin5浏览0评论
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 question

I 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 question

I 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
Add a comment  | 

1 Answer 1

Reset to default 1

You 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>';
}
发布评论

评论列表(0)

  1. 暂无评论