【问题标题】:Category pagination not working in custom post type类别分页在自定义帖子类型中不起作用
【发布时间】:2023-04-07 00:00:01
【问题描述】:

我在分页结果中显示我的类别页面时遇到问题

我有一个名为“视频”的自定义帖子类型

我把所有这种类型的帖子放在一个名为“汽车视频”的类别中

我使用模板页面 category-9.php' 因为这个类别 'cars video' 的 id 是 '9'

我将此代码放在 category-9.php 中

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>
    <?php query_posts( $query_string . '&post_type=videos&posts_per_page=10&paged=' . $paged ); ?>
    <?php if ( have_posts() ) : ?>
        <?php
        /* Start the Loop */
        while ( have_posts() ) : the_post();
            /* Include the post format-specific template for the content. If you want to
             * this in a child theme then include a file called called content-___.php
             * (where ___ is the post format) and that will be used instead.
             */
            get_template_part( 'content', 'videos' );

        endwhile;

        // twentytwelve_content_nav( 'nav-below' );
        ?>
    <?php paging(); ?>
    <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
    <?php endif; ?>

我的第一页 'sitename.com/blog/category/cat-videos/' 工作正常,但我的这些页面无法正常工作

'sitename.com/blog/category/cat-videos/page/1'

'sitename.com/blog/category/cat-videos/page/2'

等等。

它给了我 404.php 页面

这是自定义帖子注册

$post_type_args = array(
'label' => 'Videos',
'labels' => array(
    'name' => 'Videos',
    'singular_name' => 'Video',
    'menu_name' => 'Videos'
),
'public' => true,
'has_archive' => true,
'hierarchical' => true,
'supports' => array('title','author','thumbnail','comments'),
'rewrite' => array('slug' => 'videos'),
'taxonomies' => array('category','post_tag')

);


register_post_type('videos',$post_type_args);

有什么建议吗?

【问题讨论】:

    标签: wordpress pagination custom-post-type


    【解决方案1】:

    我对你的有一些问题,我找到了将这段代码放入 function.php 的解决方案。一切正常

        function fix_category_pagination($qs){
        if(isset($qs['category_name']) && isset($qs['paged'])){
            $qs['post_type'] = get_post_types($args = array(
                'public'   => true,
                '_builtin' => false
            ));
            array_push($qs['post_type'],'post');
        }
        return $qs;
    }
    add_filter('request', 'fix_category_pagination');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 2017-02-01
      • 1970-01-01
      • 2014-06-02
      相关资源
      最近更新 更多