简体   繁体   中英

WP Multiple Loops - Most Recent Post & Pagination

I'm hoping someone can help me figure this one. It's been giving me problems for a bit. I'm attempting to have a custom category page display the most recent post at the beginning of the page, and below in a separate loop I wish to display the rest of the posts in the given category WITH pagination, offsetting the loop by 1. I've searched numerous ways to do this and can't quite come up with a solution! Any help you can give for this one I'd be soooo grateful for! Thank you in advance! Here's my code:

<?php query_posts( 'cat=3&showposts=1&offset=0'); ?>
<?php while (have_posts()) : the_post(); ?>
      <div class="firstPost">
        Snipped post style
    </div>
    <div class="prevHeader">Previous Episodes</div>           
<?php endwhile; ?>
<div class="container archiveContainer">
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// the query
$the_query = new WP_Query( 'cat=3&posts_per_page=6&offset=1&paged=' . $paged ); 
?>

<?php if ( $the_query->have_posts() ) : ?>

<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<div class="prevEntries">
        Snipped Post Style
            </div>
<?php endwhile; ?> 
</div>
<div style="width:100%">         
<span style="display:inline-block; margin-left:10px; float:left" class="nav-previous"><?php next_posts_link( '<h4>Older posts</h4>', $the_query->max_num_pages ); ?></span>
<span style="display:inline-block; margin-left:10px; float:right" class="nav-next alignright"><?php previous_posts_link( '<h4>Newer posts</h4>' ); ?></span></div>
<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>

<?php endif; ?>

This can certainly be done, but surely not with query_posts . It a case like this, it will outright fail. Also, offsets breaks pagination and are quite difficult to work with if you don't know what you are doing

I don't have time to code now, but here is an idea:

If you need to display the latest post on all your pages, then you can do the following.

  • Remove you custom query and your offset so that everything works normal. Make use of the default loop and paginate as normal

  • To display your first post on every page, create a custom query with WP_Query and place it where you need to display this post .See the link on how to properly construct that query. Please do not use query_posts

  • You will now see that you have the first post displayed twice. To counter this, wrap your custom query in a is_paged() condition.

EDIT

You can try something like this. ( PS! showposts is depreciated, use posts_per_page . Also, you can removed the paged parameter when setting an offset as it will be ignored )

( CAVEAT Untested )

if( is_paged() ) {
    $args = array(
        'posts_per_page' => 1,
        'cat' => 3,
    );

    $q = new WP_Query( $args );

    if( $q->have_posts() ) {
        while( $q->have_posts() ) {
        $q->the_post();

        // Display your loop elements

        } //end while
    wp_reset_postdata();
    } // end if have_posts()
} //end if is_paged

// Now for your main loop 

// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$the_query = new WP_Query( 'cat=3&posts_per_page=6&paged=' . $paged ); 

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) { 
    $the_query->the_post();

    // Your loop elements

    }
    wp_reset_postdata();
} 

Codex states : Specifying hard-coded offsets in queries can and will break pagination since offset is used by WordPress internally to calculate and handle pagination.

See the following link . As described on the page you'll need to use a hook and filter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM