1个自定义分类中的2个自定义POST类型分页

时间:2011-05-20 作者:Ünsal Korkmaz

我有1个重要的分类法,其中包含2个不同的自定义帖子类型内容。我想以某种方式将它们显示在同一个分类页面中,同时显示两个分页。好我不确定我能不能做到。是否可以在同一分类法中分页2个不同的CPT?

2 个回复
最合适的回答,由SO网友:marfarma 整理而成

您可以使用分页创建两个单独的循环,并将它们并排放置在div容器中,每个div中都有分页链接。您可以在每个div中包含分页链接,因为一种post类型可能比另一种多,因此页面也会比另一种多。

除非您使用ajax javascript独立刷新div的内容,否则这些列将一起分页,在这种情况下,您必须在调用中传递正确的相对页码。

如果没有ajax,使用单独的post类型列串联分页,您的模板将如下所示(未测试的代码):

<?php
/*
 Template Name: Side-by-Side Paginated Custom Post Types
*/
  global $query_string;
  global $wp_query;

  get_header(); 

  $myquery = wp_parse_args($query_string);
  $myquery[\'tax_query\'] = $wp_query->tax_query;

  echo \'<div id="container">\';

    // Work whether query var is page or paged
    if ( get_query_var(\'paged\') ) 
      { $paged = get_query_var(\'paged\'); }
    elseif ( get_query_var(\'page\') ) 
      { $paged = get_query_var(\'page\');
    } else 
      { $paged = 1; }

    $myquery[\'post_status\']  = \'publish\';
    $myquery[\'paged\']        = \'.$paged\';

    // Create Page Heading, including each term from the query
    // Allows for advanced tax query with more than one taxonomy term, such as News and Events in the same query

    foreach ($wp_query->tax_query->queries as $taxterm) {
     if (\'your_taxonomy\' == $taxterm[\'taxonomy\']) {

       $last_term = end( array_keys( $taxterm[\'terms\'] ) ); // Mark the last topic

         echo "<h1>";
         foreach ($taxterm[\'terms\'] as $k => $termid) {
           $termobj = get_term_by( $taxterm[\'field\'], $termid, $taxterm[\'taxonomy\'] );
           echo $termobj->name;
           if ( $k !== $last_term ) echo \' &amp; \';
         }
         echo "</h1>";
     }
    }

    echo \'<div class="left-column">\'; 

      $myquery[\'post_type\']    = \'your_cpt_1\';

      echo \'<h2 class="sec1 title">Custom Posts 1</h2>\';
      // Query
      $myquery = new WP_query( $args );
        if ( $myquery->have_posts() ) : while ( $myquery->have_posts() ) : $myquery->the_post(); 

      get_template_part( \'content\', \'your_cpt_1\' );

      endwhile; else :
      endif;

      echo \'<div class="pagination">\';
        echo \'<div class="alignleft">\'; previous_posts_link(\'Previous\'); echo \'</div>\';
        echo \'<div class="alignright">\'; next_posts_link(\'Next\'); echo \'</div>\';
      echo \'</div> <!-- end pagination -->\';

      wp_reset_postdata();      

    echo \'</div> <!-- div class="left-column" -->\'; 

    echo \'<div class="right-column">\'; 

      $myquery[\'post_type\']    = \'your_cpt_2\';
      echo \'<h2 class="sec1 title">Custom Posts 2</h2>\';
      // Query
      $myquery = new WP_query( $args );
        if ( $myquery->have_posts() ) : while ( $myquery->have_posts() ) : $myquery->the_post(); 

      get_template_part( \'content\', \'your_cpt_2\' );

      endwhile; else :
      endif;

      echo \'<div class="pagination">\';
        echo \'<div class="alignleft">\'; previous_posts_link(\'Previous\'); echo \'</div>\';
        echo \'<div class="alignright">\'; next_posts_link(\'Next\'); echo \'</div>\';
      echo \'</div> <!-- end pagination -->\';

      wp_reset_postdata();      
    echo \'</div> <!-- div class="right-column" -->\'; 

  echo \'</div> <!-- div id="container" -->\'; 

get_footer();
?>

SO网友:Lee Peterson

我想你是说你有两种帖子类型,每种都有相同的分类术语。

创建查询帖子类型和单个分类法的模板:

query_posts( array(\'post_type\' => array(\'post_type_one\', \'post_type_two\'), \'tax_slug\' => \'term_name\' ) );

结束

相关推荐

Pagination with custom loop

我的问题可能是Pagination not working with custom loop, 但有一种不同。我使用自定义循环来显示flash游戏。我想按类别在游戏页面上分页。类别php:<?php if ($cat) { $cols = 2; $rows = 4; $paged = ((\'paged\')) ? get_query_var(\'paged\') : 1; $post_per_page = $cols * $rows; // -1 s