0

In my main loop I want to add a custom query after the first 3 posts. I managed to do this -- code will follow -- but I have one problem: instead of adding the new posts after the third post, my code will replace the third post from the main loop with the custom query posts.

The question: What do I have to modify so that the custom query posts will be added after the third post of the main loop and not replace it?

<?php if ( have_posts() ) : ?>
        <?php $count = 0; ?>
            <?php while ( have_posts() ) : the_post(); ?>
                    <?php $featured = get_theme_mod( 'featured' );
                        if ( $featured == 'yes' && $count == 3 && get_theme_mod('masonry') == false ) {
                            get_template_part( 'featured', 'posts' );
                        } else { 
                        $masonry = get_theme_mod('masonry');
                        if ( $masonry == '' ) {
                            get_template_part( 'content', get_post_format() );
                        } else {
                            get_template_part( 'content', 'masonry' );
                        }
                    ?>
                <?php } ?>
                <?php $count++; ?>
            <?php endwhile; ?>
            <!-- // pagination in footer.php  -->    
        <?php else : ?>
            <?php get_template_part( 'content', 'none' ); ?>
        <?php endif; ?>

Now, in my featured-posts.php I have this query:

$the_query = new WP_Query( array( 'category_name' => 'featured', 'ignore_sticky_posts' => 1, 'posts_per_page' => 2 ) );

    // The Loop
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) :
            $the_query->the_post(); 
            $format = get_post_format(); ?>             
        endwhile;
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
  • 1
    don't put normal post output inside else? you don't want one or the other, you want both. – Milo Dec 22 '14 at 23:15
  • You're right, thanks! Don't know how I didn't saw that. – George Grigorita Dec 22 '14 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.