分页不适用于自定义帖子类型

时间:2012-11-09 06:45:05

标签: php wordpress wordpress-plugin wordpress-theming

我在自己的网站上创建了自定义帖子类型“投资组合”。我能够创建类别,帖子等等。我创建了类别页面,根据类别显示所有帖子,我通过在archive.php中使用以下代码块来完成它。

<?php $cat = get_query_var('cat');?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
 <?php query_posts('showposts=3&post_type=portfolio&category_name=web-design&order=ASC&paged='.$paged); ?>    
 <div id="page" class="category_post grid_12">    
<?php while (have_posts() ) : the_post(); ?>
<div class="grid_3 single-post">
<div class="list-post">
<a class="read-more" href="<?php echo get_permalink()?>"><?php echo                   the_post_thumbnail(array(213,185));?></a>
</div>
 <p>
 <?php $content=trim(get_field("description"));
  echo $half_content=substr($content,0,88)."..." ?>
  <a class="category-link" href="<?php echo get_permalink()?>">Read more</a>
  </p>
</div>
  <?php endwhile; ?>
  <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>

我也使用wp_pagenavi插件进行分页。现在我的问题是,当我点击第2页,即下一页时,它不会在其上显示较新的帖子。请告诉我上面的代码有什么问题。

2 个答案:

答案 0 :(得分:0)

您的自定义帖子类型是否已将has_archive属性设置为true?您是否使用存档模板或tring来查询模板代码中的帖子?

答案 1 :(得分:0)

        Add custom_pagination function in your function.php file 

         `function custom_pagination($numpages = '', $pagerange = '', $paged='') {
             if (empty($pagerange)) {
                 $pagerange = 2;
             }
             global $paged;
                if (empty($paged)) {
                 $paged = 1;
             }
            if ($numpages == '') {
               global $wp_query;
                $numpages = $wp_query->max_num_pages;
                if(!$numpages) {
                 $numpages = 1;
               }
           }
           $pagination_args = array(
               'base'            => get_pagenum_link(1) . '%_%',
               'total'           => $numpages,
               'current'         => $paged,
              'show_all'             => False,
              'end_size'             => 1,
              'mid_size'             => $pagerange,
              'prev_next'        => False,
              'prev_text'        => __('&laquo;' , 'swift'),
              'next_text'        => __('&raquo;' , 'swift'),
              'type'             => 'array',
              'add_args'             => false,
              'add_fragment'     => ''
             ); 
              $paginate_links = paginate_links($pagination_args);
               echo '<div class="Pagination-Num"><ul>';
                  foreach ( $paginate_links as $paginate_link ) {
                         echo "<li> $paginate_link </li>";
                    }
                echo '</ul></div>';
        }
         `




  And In your Custom page template add the following code...

  <?php
    /**
     * Template Name: Custom Page
     * The custom page template file
     */
 ?>

    <?php get_header(); ?>

    <h2>Posts</h2>

    <?php 

      $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

      $custom_args = array(
          'post_type' => 'post',
          'posts_per_page' => 2,
          'paged' => $paged
        );

      $custom_query = new WP_Query( $custom_args ); ?>

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

        <!-- the loop -->
        <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
          <article class="loop">
            <h3><?php the_title(); ?></h3>
            <div class="content">
              <?php the_excerpt(); ?>
            </div>
          </article>
        <?php endwhile; ?>
        <!-- end of the loop -->

        <!-- pagination here -->
        <?php
          if (function_exists(custom_pagination)) {
            custom_pagination($custom_query->max_num_pages,"",$paged);
          }
        ?>

      <?php wp_reset_postdata(); ?>

      <?php else:  ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
      <?php endif; ?>

    <?php get_footer(); ?>

我相信它会起作用。