0

我在我的网站中创建了自定义帖子类型“投资组合”。我能够创建类别、帖子...等。我创建了类别页面,该页面根据类别显示所有帖子,我通过使用 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 页即下一页时,它不会显示较新的帖子。请告诉我上面的代码有什么问题。

4

2 回答 2

0

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

于 2012-11-13T20:49:56.100 回答
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(); ?>

我相信它会起作用的。

于 2014-12-09T06:21:12.007 回答