【问题标题】:WP Custom taxonomy paginationWP自定义分类分页
【发布时间】:2014-05-22 20:48:06
【问题描述】:

我有一个自定义分类(支持)和一个自定义帖子类型(问题),两者都相关。

在我的 taxonomy-support.php 模板文件中,我使用以下查询:

<?php

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

$questions = new WP_Query(array(
    'post_type' => array('question'),
    'post_status' => 'publish',
    'posts_per_page' => 2,
    'paged' => ((get_query_var('paged')) ? get_query_var('paged') : 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>
        <?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; ?>
        <?php wp_reset_postdata(); ?>
    </ul>

    <div class="clearfix">
        <div class="pull-left">
            <?php previous_posts_link('&larr; ' . __('Previous', 'my-theme' ), $questions->max_num_pages); ?>
        </div>
        <div class="pull-right">
            <?php next_posts_link(__('Next', 'my-theme') . ' &rarr;', $questions->max_num_pages); ?>
        </div>
    </div>

<?php endif; ?>

如您所见,我每页定义了 2 个帖子。

当我访问该页面时,它显示了 2 个帖子,然后我转到第 2 页,它仍然有效,但是当我转到第 3 页或更高页面时,它显示 404。

有什么想法吗?

我正在使用没有安装插件的 WordPress 3.8.2。

谢谢

【问题讨论】:

    标签: wordpress pagination taxonomy


    【解决方案1】:

    我很高兴找到您的帖子,因为我正在为完全相同的问题而战(不太高兴找到没有答案;))。

    不管怎样,我终于在这里找到了一些有趣的东西:https://wordpress.org/support/topic/set-number-of-posts-for-custom-taxonomy

    这对我来说是个窍门。

    我认为您的全局 posts_per_page 选项(在管理员中设置)的值大于您的分类的值,这使系统搞砸了。

    好吧,当您考虑它时并没有那么多:在其生命周期中,具有全局 posts_per_page 选项,请求不会验证“分页” url,因此甚至永远不会加载您的 taxonomy-support.php 模板文件,你告诉它 posts_per_page 应该不同。

    然后您必须在初始化过程中更早地告诉它。 'pre_get_posts' 操作就是其中之一。

    希望它对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 2012-06-21
      • 1970-01-01
      相关资源
      最近更新 更多