【问题标题】:WordPress - custom post type paginationWordPress - 自定义帖子类型分页
【发布时间】:2014-05-21 02:09:36
【问题描述】:

我有一个与自定义分类(支持)相关的自定义帖子类型(问题)

在 my-theme/taxonomy.php 我有这个代码:

<?php

$taxonomy = get_queried_object()->taxonomy;

if ($taxonomy == 'support')
{
    get_template_part('template/support/categories');
    exit;
}

wp_safe_redirect(site_url('/'));
exit;

?>

意味着我针对“支持”分类法的特定模板文件。

在我的分类模板文件中,我进行了自定义查询:

<?php $current_category = get_term_by('id', get_queried_object()->term_id, 'support'); ?>

<?php $questions = new WP_Query(array(
    'post_type' => array('question'),
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'posts_per_archive_page' => 5,
    'paged' => ((get_query_var('page')) ? get_query_var('page') : 1),
    'nopaging' => false,
    'tax_query' => array(
        array(
            'taxonomy' => 'support',
            'terms' => array($current_category->term_id)
        )
    ),
    'orderby' => 'menu_order',
    'order' => 'ASC'
)); ?>

<?php if ($questions->have_posts()): ?>
    <ul class="list normalize">
        <?php while ($questions->have_posts()) : $questions->the_post(); ?>
            <li>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <?php the_excerpt(); ?>
            </li>
        <?php endwhile; ?>
    </ul>
<?php endif; ?>

<div class="nav-previous">
    <?php next_posts_link( __('Previous', 'my-theme'); ?>
</div>

<div class="nav-next">
    <?php previous_posts_link( __('Next', 'my-theme'); ?>
</div>

<?php wp_reset_postdata(); ?>

我有大约 11 个帖子,第 1 页显示了前 5 个帖子,但问题是没有显示分页。

有什么想法吗? 谢谢

【问题讨论】:

    标签: wordpress pagination custom-post-type


    【解决方案1】:

    你可以试试这个分页插件

    http://wordpress.org/plugins/wp-pagenavi/
    

    并将此代码添加到您的模板中

    <?php if (!function_exists('wp-pagenavi')) { wp_pagenavi(); } else { ?>
        <div class="navigation">
        <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
        <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    </div><?php } ?>
    

    【讨论】:

      【解决方案2】:

      我尝试了您的代码和我的分类法

      <?php $questions = new WP_Query(array(
          'post_type' => array('studies'),
          'post_status' => 'publish',
          'posts_per_page' => 15,    
          'paged' => get_query_var('paged'),
          'nopaging' => false,
          'tax_query' => array(
              array(
                  'taxonomy' => 'studies',
                  'terms' => 11
              )
          ),
          'orderby' => 'menu_order',
          'order' => 'ASC'
      ));
      
      
      ?>
      
      <div class="grid9">
      <div class="entry-content">
      <?php if ($questions->have_posts()): ?>
          <ul class="list normalize">
              <?php while ($questions->have_posts()) : $questions->the_post(); ?>
                  <li>
                      <h3><a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></h3>                
                  </li>
              <?php endwhile; ?>
          </ul>
      
      <?php endif; ?>
      <?php
      wp_pagenavi( array( 'query' => $questions ) ); 
      ?>
      

      这很好用,别忘了使用 wp-pagenavi 插件。

      【讨论】:

      • 谢谢,但是您在使用分页导航时没有出现 404 错误页面吗?
      • 欢迎您,不,我在导航时没有收到 404 错误。
      • 天哪,我对那个分页快疯了,找不到问题。
      • 尝试更改您的页面模板名称并检查,这可能是问题所在。 wordpress.stackexchange.com/questions/92194/…
      • 你的意思是 /* 模板名称: xxx */ ?
      【解决方案3】:

      将此代码放在“endif;”之上就在while循环结束的下方。

       <div class="nav-previous">
          <?php next_posts_link( __('Previous', 'my-theme'); ?>
      </div>
      
      <div class="nav-next">
          <?php previous_posts_link( __('Next', 'my-theme'); ?>
      </div>
      
      <?php wp_reset_postdata(); ?>
      

      希望对你有所帮助。

      【讨论】:

      • 我只是尝试使用 wp_pagenavi 插件,它可以工作,但是当切换到第 2 页时,我得到了 404 $questions)) ?>
      • 同时传递 2 个参数,Paged 和 Posts per page...这两个对于分页至关重要..
      • 好的,我在 next_posts_link() 中使用了 $max_pages 参数,它显示了分页链接,但在访问第 2 页时出现 404 错误。有什么想法吗?
      【解决方案4】:

      通常这对我有用: $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $questions->max_num_pages ) );

      注意问题变量。 :-) 使用后,转到设置-> 固定链接并保存两次。

      您可以找到更大的教程here 使用相同的示例。

      【讨论】:

        猜你喜欢
        • 2014-05-15
        • 2011-03-20
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        • 2016-08-08
        • 2012-11-29
        • 2017-06-04
        • 2013-06-22
        相关资源
        最近更新 更多