Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

http://www.ttmt.org.uk

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.

    <?php

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

        $frontLoop = new WP_Query($frontAgrs);

        if($frontLoop->have_posts()):
            while($frontLoop->have_posts()):
                $frontLoop->the_post();

        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(); ?>           
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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 = '':
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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