0

Recently I made a theme for my WordPress blog. Now I want to filter(remove) my 5 latest post from the main page (index.php) and show them in a slider. Now my 5 latest posts are showing in the first page and slider simultaneously :(

Update:

Thanks for your reply. I couldn't solve my problem. this is my index.php source:

<?php get_header() ?>

<div id="main-wrapper">
    <div class="middle-content">
        <div id="sidebar-right">
            <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-right')) : else : ?>
            <?php endif; ?>
        </div>
        <section class="clearfix" id="content">
            <section id="main-content">
                <?php echo get_touchcarousel(2); ?>
                <div class="mainnews"><h2>Latest News</h2></div>
                <div class="main-menu-content">
                    <?php if(have_posts()) : ?>
                        <?php while (have_posts()) : the_post(); ?>
                            <article id="etc" class="post<?php the_category_unlinked(' '); ?>">
                                <div class="post-content">
                                    <div class="post-title">
                                        <a href="<?php the_permalink(); ?>"><h1><?php the_title(); ?></h1></a>
                                    </div>
                                    <div class="clear"></div>
                                    <?php the_excerpt(); ?>
                                    <?php the_post_thumbnail(array(260,140), array ()); ?>
                                </div>
                                <div class="clear"></div>
                            </article>

                        <?php endwhile; ?>
                    <?php endif; ?>
                </div>
                <?php if(function_exists('wp_simple_pagination')) {
                    wp_simple_pagination();
                } ?>
            </section> 
            <div id="sidebar-left">
                <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-left')) : else : ?>
                <?php endif; ?>
            </div>
        </section>
    </div>
    <div class="clear"></div>
    <?php get_footer(); ?>
</div>
</section>
</div>
<div class="clear"></div>
<?php get_footer(); ?>

Would you please help me?

3
  • can you provide the code of the first page template? is the slider code in the same template as the loop?
    – Michael
    Commented Dec 7, 2012 at 11:31
  • You probably need to add wp_reset_postdata() or wp_reset_query() at the end of your loop.
    – RRikesh
    Commented Dec 7, 2012 at 12:24
  • Please check whether you have added wp_reset_query() while ending the loop. Moreover, you can also check whether you have added offset attribute properly to your query_posts.
    – swtshweta
    Commented Dec 7, 2012 at 12:35

3 Answers 3

1

As @getglad says you could build in a counter to skip the first returned posts, but it would be better to loop through a separate query like this:

// show ten posts, begin with the fifth
$the_query = new WP_Query( array( 'posts_per_page' => 10, 'offset' => 5 ) );

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<article>' ... your stuff ... '</article>';
        }
    /* Restore original Post Data */
    wp_reset_postdata();
    }
else {
    // no posts found
    }
0

I don't know if WP has a built in feature for it's query but you could do something like this:

$count = 0;
if(have_posts()) : while (have_posts()) : the_post();
if ($count < 4 ) { $count++; }
else { ... your blogroll code ... } 
endwhile; endif;
0

To remove the 5 latest posts from the loop in WordPress, you can use the pre_get_posts action hook to modify the query before it is executed. Here's an example of the code you can use in your functions.php file:

function exclude_latest_posts( $query ) {
  if ( $query->is_home() && $query->is_main_query() ) { // modify only the main query on the home page
    $query->set( 'offset', 5 ); // exclude the 5 latest posts
  }
}
add_action( 'pre_get_posts', 'exclude_latest_posts' );

In this code, we're using the pre_get_posts action hook to modify the main query on the home page. We're checking if the query is the main query ($query->is_main_query()) and if it's on the home page ($query->is_home()). Then we're setting the offset parameter to 5, which excludes the 5 latest posts from the loop.

Note that this code modifies the main query, so it may affect other loops on the home page as well. If you need to exclude the 5 latest posts only from a specific loop, you can use a custom query instead.

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.