douben8492 2012-12-06 17:01
浏览 47
已采纳

如何为多个循环创建单独的分页?

I have created two loops with pagination (first loop loops through CAT'S category and second loops through DOG'S category), but now I am stuck:(

The problem: After I click "Next entry" on my site (CAT'S category) it goes to second entry in that category BUT it also goes to my DOG'S category second entry (I don't want THAT!! ). It also happens vice versa...

What I like to do is this: I click on "Next Entry" on my CAT'S category and it goes only to next post in THAT category (CAT'S) but NOT to second post in my DOG'S category, or another way around: I click on "Next Entry" on my DOG'S category and it goes only to next post in THAT category (DOG'S) but NOT to second post in my CAT'S category .

Can someone help me please? I have asked for help on wordpress.stackexchange.com a while ago but I didn't get any answer so I am asking question here.

Index php looks like this:

<?php get_header(); ?>
<?php get_sidebar(); ?>
<div id="blog">         
    <?php         
    $args = array(
    'category_name' => 'cats' 
    );
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = new WP_query($args . '&paged=' . $paged . '&cat=-3');      
    while( $the_query -> have_posts()) : $the_query -> the_post();              
    ?>

    <div class="post">
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content('Read on...'); ?>

            <p class="postmetadata">
            <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>
    </div>

    <?php endwhile;?>
    <?php wp_reset_postdata();?>
    <div class="navigation">
    <div style="float:left;" class="alignleft"><?php previous_posts_link('&laquo; Previous Entries') ?></div>
    <div style="float:right;" class="alignright"><?php next_posts_link('Next Entries &raquo;',$the_query->max_num_pages) ?></div>
    </div>      
    </div>

    <div id="blogs">    
    <?php       
    $args = array(
    'category_name' => 'dogs' 
    );
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = new WP_query($args . '&paged=' . $paged . '&cat=-10');
    while( $the_query -> have_posts()) : $the_query -> the_post();              
    ?>

    <div class="post">
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content('Read on...'); ?>

            <p class="postmetadata">
            <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>

        </div>
    </div>

    <?php endwhile;?>
    <?php wp_reset_postdata();?>
    <div class="navigation">
    <div style="float:left;" class="alignleft"><?php previous_posts_link('&laquo; Previous Entries') ?></div>
    <div style="float:right;" class="alignright"><?php next_posts_link('Next Entries &raquo;',$the_query->max_num_pages) ?></div>
    </div>
    </div>
   <?php get_footer(); ?>
  • 写回答

4条回答 默认 最新

  • doufei6456 2012-12-14 10:38
    关注

    You need 2 different paging values so add some new ones and rewrite rules which look for them (they're really just to make the urls neater looking). The rewrite rules and the pagination link format mean you can page through one category while the other category page doesn't change.

    In functions.php:

    function add_new_rules()
    {
      // new 'paged' variables
      global $wp;
      $wp->add_query_var('paged_cats');
      $wp->add_query_var('paged_dogs');
    
      // rewrite rules
      add_rewrite_rule('page/cats/(\d+)/dogs/(\d+)', 'index.php?paged_cats=$matches[1]&paged_dogs=$matches[2]', 'top');
      add_rewrite_rule('page/cats/(\d+)/dogs/?$', 'index.php?paged_cats=$matches[1]&paged_dogs=1', 'top');
    
      if( !array_key_exists('page/cats/(\d+)/dogs/(\d+)', (array)get_option('rewrite_rules')) )
      {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
      }
    }
    add_filter('init', 'add_new_rules');
    

    Check for the new query vars in index.php and use them for each WP_Query and the related pagination links.

    <div id="blog">
      <?php
      $paged_cats = (get_query_var('paged_cats')) ? get_query_var('paged_cats') : 1;
      $paged_dogs = (get_query_var('paged_dogs')) ? get_query_var('paged_dogs') : 1;
    
      $cats = new WP_query(array(
        'category_name' => 'cats',
        'paged' => $paged_cats,
        'posts_per_page' => 1
      ));
      while( $cats->have_posts() ) : $cats->the_post();
        ?>
        <div class="post">
          <div class="post_title">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
          </div>
          <div class="entry">
            <?php the_post_thumbnail(); ?>
            <?php the_content('Read on...'); ?>
            <p class="postmetadata">
              <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
              <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
          </div>
        </div>
      <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>
      <?php if ( $cats->max_num_pages > 1 ) : ?>
      <div class="navigation">
        <?php
        echo paginate_links(array(
          'base' => home_url("page/cats/%#%/dogs/{$paged_dogs}"),
          'format' => '%#%',
          'current' => $paged_cats,
          'total' => $cats->max_num_pages,
        ));
        ?>
      </div>
      <?php endif; ?>
    </div>
    
      <hr>
    
    <div id="blogs">
      <?php
      $dogs = new WP_query(array(
        'category_name' => 'dogs',
        'paged' => $paged_dogs,
        'posts_per_page' => 1
      ));
      while( $dogs->have_posts() ) : $dogs->the_post();
        ?>
        <div class="post">
          <div class="post_title">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
          </div>
          <div class="entry">
            <?php the_post_thumbnail(); ?>
            <?php the_content('Read on...'); ?>
            <p class="postmetadata">
              <?php _e('Filed under&#58;'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
              <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
          </div>
        </div>
      <?php endwhile;?>
      <?php wp_reset_postdata();?>
      <?php if ( $dogs->max_num_pages > 1 ) : ?>
      <div class="navigation">
        <?php
        echo paginate_links(array(
          'base' => home_url("page/cats/{$paged_cats}/dogs/%_%"),
          'format' => '%#%',
          'current' => $paged_dogs,
          'total' => $dogs->max_num_pages,
        ));
        ?>
      </div>
      <?php endif; ?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测