2
<?php $terms = wp_get_post_terms($post->ID,'category');  
            foreach ($terms as $term) {  
            $termcomp = $term->taxonomy . '_' . $term->term_id; } ?>


            <?php the_field('tagline' , $termcomp); ?>

我如何在 wordpress 上使用此代码而不回显 6 次,因为我在该类别中有 6 个帖子..

http://pastebin.com/ijqwA5SK

整页模板在那里,foreach 也在底部,这个工作正常,只根据需要输出一次。

4

1 回答 1

0

使用高级自定义字段,您可以使用 获取类别分类自定义字段的字段get_field('field', 'category_'.$cat_id),或者the_field()如果您想自动回显结果。在您的情况下,您首先需要确定当前的类别 ID,然后the_field()使用它调用您想要的字段名称,tagline. 这应该在外部加载Loop

// only show on category pages
if(is_category()){
    global $wp_query;
    // get category id from query variables
    $cat_ID = get_query_var('cat');
    the_field('tagline', 'category_'.$cat_ID);
}
于 2012-10-29T21:25:55.297 回答