Foreach循环仅适用于wordpress循环,但回显6次。我可以在循环外使用它吗?

时间:2012-10-29 18:15:45

标签: wordpress foreach custom-post-type

<?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也在底部,这个工作正常,只需要输出一次。

1 个答案:

答案 0 :(得分: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);
}