WP多圈 - 最新帖子和分页

WP多圈 - 最新帖子和分页

问题描述:

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; ?>

我希望有人可以帮我解决这个问题。 它给了我一些问题。 我正在尝试让自定义类别页面显示页面开头的最新帖子,而在下面的单独循环中我希望显示给定类别WITH分页中的其余帖子,将循环偏移1。 我已经搜索了很多方法来做到这一点,并没有找到解决方案! 你可以为这个提供任何帮助,我会非常感激! 先感谢您! 这是我的代码: p>

 &lt;?php query_posts('cat = 3&amp; showposts = 1&amp; offset = 0');  ?&gt; 
&lt;?php while(have_posts()):the_post();  ?&gt; 
&lt; div class =“firstPost”&gt; 
 Snipped post style 
&lt; / div&gt; 
&lt; div class =“prevHeader”&gt; Previous Episodes&lt; / div&gt;  
&lt;?php endwhile;  ?&gt; 
&lt; div class =“container archiveContainer”&gt; 
&lt;?php 
 //设置“paged”参数(如果查询位于静态首页,请使用“page”)
 $ paged =  (get_query_var('paged'))?  get_query_var('paged'):1; 
 
 //查询
 $ the_query = new WP_Query('cat = 3&amp; posts_per_page = 6&amp; offset = 1&amp; paged ='。$ paged);  
?&gt; 
 
&lt;?php if($ the_query-&gt; have_posts()):?&gt; 
 
&lt;?php 
 // loop 
while($ the_query-&gt; have_posts(  )):$ the_query-&gt; the_post();  
?&gt; 
&lt; div class =“prevEntries”&gt; 
 Snipped Post Style 
&lt; / div&gt; 
&lt;?php endwhile;  ?&GT;  
&lt; / div&gt; 
&lt; div style =“width:100%”&gt;  
&lt; span style =“display:inline-block; margin-left:10px; float:left”class =“nav-previous”&gt;&lt;?php next_posts_link('&lt; h4&gt;旧帖子&lt; / h4&gt;'  ,$ the_query-&gt; max_num_pages);  ?&gt;&lt; / span&gt; 
&lt; span style =“display:inline-block; margin-left:10px; float:right”class =“nav-next alignright”&gt;&lt;?php previous_posts_link('&lt;  h4&gt;较新的帖子&lt; / h4&gt;');  ?&gt;&lt; / span&gt;&lt; / div&gt; 
&lt;?php 
 //在查询和分页后清理
 
wp_reset_postdata();  
?&gt; 
 
&lt;?php endif;  ?&gt; 
  code>  pre> 
  div>

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.