dongyun6229 2014-11-13 05:22
浏览 52
已采纳

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; ?>
  • 写回答

2条回答 默认 最新

  • dongxie9448 2014-11-13 08:14
    关注

    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();
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)