Foreach loop only works inside wordpress loop, but echoes 6 times. Can I use it outside the loop?

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


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

How Can I use this code on wordpress without it echoing 6 times, because I have 6 posts inside that category..

http://pastebin.com/ijqwA5SK

Read More

Full page template is there, the foreach is also at the bottom, this one works fine and only outputs once as needed.

Related posts

Leave a Reply

1 comment

  1. Using Advanced Custom Fields, you can get the field for a category taxonomy custom field using the get_field('field', 'category_'.$cat_id), or the_field() if you want to echo the results automatically. In your case, you first need to determine the current category id, and then call the_field() using it for your desired field name, tagline. This should be loaded outside of the 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);
    }