I have a rough demo here to try and illustrate my problem.


I have six pages with titles - Page One, Two etc.

On a index/home page I'm creating a btn/div to link to each page.

On one of the btn's I want to put an extra label/mark.

On the page connected to that btn I have created a custom field with the name of label.

In the loop I am checking for this custom field with get_post_meta. If it's there I'm adding a to the div.

My problem is the is added to the other div's after the after the div/page that has the 'label' custom field.

In the example page http://www.ttmt.org.uk

I want the added label on Page Three but it also appearing on the after that.


        $frontAgrs = array(
            'post_type' => 'page',
            'tag' => 'group',
            'orderby' => 'menu_order',
            'order' => 'ASC'

        $frontLoop = new WP_Query($frontAgrs);


        if(get_post_meta($post->ID, 'label', true) != ''){
            $label = get_post_meta($post->ID, 'label', true);
            $label = '<em>'.$label.'</em>';

        $btnName = get_the_title();


    <?php echo '<div class="theDiv" ><a href="'.get_permalink($post->ID).'"><span>'.$btnName.'</span>'.$label.'</a></div>' ?>

    <?php endwhile; endif; ?>

    <?php wp_reset_postdata(); ?>           

1 Answer 1


You need to put an exception into your conditional:

if(get_post_meta($post->ID, 'label', true) != ''){
   $label = get_post_meta($post->ID, 'label', true);
   $label = '<em>'.$label.'</em>';
} else {
   $label = '':

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.