0

I've made a custom news page using a custom page template and this is my query for latest posts:

if ( have_posts() ) : 
    while ( have_posts() ) : the_post();
        the_content(); ?>
        <div class="line2"></div>
    <?php endwhile; 
endif;
wp_reset_query(); ?>

<!-- Start latest post -->
<?php $latest_post = get_posts( 'numberposts=5' ); // Defaults args fetch posts starting with the most recent
foreach( $latest_post as $post ) : setup_postdata( $post ); ?>
    <div class="newsthumb">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
    </div>
    <div class="newstitle">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </div>
    <div class="postdate-category">
        <?php esportsheaven_posted_on(); ?> | <?php the_category(); ?>
    </div>
    <div class="newscontent">
        <?php the_excerpt(); ?>
    </div>
<?php endforeach;
wp_reset_query();

There should be 5 posts initially on the page and when the site gets more then 5 posts, I'm looking to add pagination. What's the best/most efficient way to do this?

0

If you are going to paginate the query in future, I recommend the following

  • Changing to WP_Query instead as it is better suited for paginated queries.

  • Although you still have less than 5 posts, you can make your query pagination ready by adding the paged parameter to your query

  • Your use of wp_reset_query() is incorrect. That is used in conjuction with query_posts which you should never use. You should be using wp_reset_postdata(). Also, you don't need to reset the main query, it is already done.

  • Just a tip, use curlies instead of : and endif and endwhile. It is easier to debug and almost all if not all code editors support this syntax

You can try something like this

if ( have_posts() ) { 
    while ( have_posts() ) { 
        the_post();

        the_content(); ?>
        <div class="line2"></div>
    <?php } 
}
?>

<!-- Start latest post -->
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$latest_post = new WP_Query( 'numberposts=5&paged=' . $paged ); // Defaults args fetch posts starting with the most recent
if ( $latest_post->have_posts() ) { 
        while ( $latest_post->have_posts() ) { 
            $latest_post->the_post(); ?>
            <div class="newsthumb">
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
            </div>
            <div class="newstitle">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </div>
            <div class="postdate-category">
                <?php esportsheaven_posted_on(); ?> | <?php the_category(); ?>
            </div>
            <div class="newscontent">
                <?php the_excerpt(); ?>
            </div>
        <?php 
    }
    wp_reset_postdata();
}
  • wp_reset_query() is used in Genesis and there's no query_posts in Genesis. – Brad Dalton Mar 12 '15 at 4:52
  • It is stupid in my opinion, why are you resetting the main query to itself? Also, Genesis is never referenced. I pointed out to the OP that wp_reset_query() is used with query_posts. It is maybe personal preference using it with the main query, but really, think about it, the main query sets the global scope values, why do you need to reset that globals to the main query if the main query was used to set it in the first place – Pieter Goosen Mar 12 '15 at 5:05
  • I agree. Just don't know why people use it when not using query_posts. – Brad Dalton Mar 12 '15 at 5:19
  • Please let me know if this solution worked so that I can assist you if not. If my solution worked, please accept my answer by clicking on the checkmark next to my answer, it will go green. Thank you :-) – Pieter Goosen Mar 13 '15 at 4:35
  • I actually figured out my own solution late last night, but it ran similar to this! This works for sure! Thanks man – Jacob Wolf Mar 13 '15 at 14:13

Your Answer

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

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