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 am writing a custom multiple loop to be used on a custom category template page. The loop should put one post that is checked as featured in admin, in a separate div, and continue the loop displaying all posts from the category except the featured.

Similar to the example provided on the codex page except I don't want to create a separate category for the featured post.

I am using Advanced Custom Fields plugin for the check box that sets posts as featured.

I have the following issue with my code: if ($post->ID == $do_not_duplicate) continue; prevents rest of the loop to be executed. The code below just pulls the latest featured post.

Here is my function:

function featured() {
$featured = new WP_Query(array(
'meta_query' => array(
    array(
        'key' => 'featured',
        'value' => '"top"',
        'compare' => 'LIKE'
        )
    ),
'posts_per_page' => 1
));

while ( $featured->have_posts() ) : $featured -> the_post(); 
$do_not_duplicate = $post->ID; ?>
<div id="featured">
    //featured post
</div><!-- end #featured -->
<?php 
endwhile;
if(have_posts()) : while (have_posts()) : the_post();
if ($post->ID == $do_not_duplicate) continue;
?>
<div class="container">
// normal posts
</div><!-- .charities-container -->
<?php 
endwhile;
endif;
}
share|improve this question
add comment

1 Answer 1

Replace $do_not_duplicate = $post->ID; with $do_not_duplicate = get_the_ID();.

After the first loop (the featured) call wp_reset_postdata();

....

</div><!-- end #featured -->
<?php 
endwhile;

wp_reset_postdata();

if( have_posts() ) : while ( have_posts() ) : the_post(); 

...
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.