在子类别上分页,在父类别上工作良好

时间:2013-01-08 作者:ByteMyPixel

我有两个父类别,分页对它们很有效-分页url显示为:

/parent-category-1/page/2/ ... etc
/parent-page/parent-category-2/page/2/ ... etc

但是,当我转到子类别时,分页返回404-for,例如:

/category/parent-category-1/child-category/page/2/ ... etc=404
/category/parent-category-2/child-category/page/2/ ... etc=404

我正在使用WP PageNavi插件进行分页parent-category-1 为我的标准博客帖子添加类别
parent-category-2 设置自定义帖子类型类别的父级

以下是以下博客帖子的模板parent-category-1 (标准模板-category.php):

<div class="feed med">
    <?php   

     $paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1; //  FOR PAGINATION

        $args = array(
           \'post_type\' => \'post\',
           \'category__in\' => ($cat),
           \'posts_per_page\' => 5, 
           \'page\' => $paged, //  FOR PAGINATION
        );

        // FOR PAGINATION - hijack the $wp_query variable temporarily
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query($args);

        $second_query = new WP_Query( $args );
        if ( $second_query->have_posts() ):
           while( $second_query->have_posts() ) : $second_query->the_post();

           $titlechars = 45000; // Character Limit - rediculously large number to eliminate limit
           $posttitle = get_the_title();
           $modtitle = substr($posttitle, 0, $titlechars);

           $contentchars = 120; // Character Limit
           $postcontent = get_the_excerpt();
           $modcontent = substr($postcontent, 0, $contentchars);

        echo \'<article>\';
    ?>

    <?php
        if( get_field(\'image\') ):

            $attachment_id = get_field(\'image\');
            $size = \'customfeatins\'; // (thumbnail, medium, large, full or custom size)
            $image = wp_get_attachment_image_src( $attachment_id, $size );

        echo \'<a href="\' . get_permalink() . \'"><img src="\' . $image[0] . \'" alt="\' . get_the_title() .\'" width="136" height="90" /></a>\';
    ?>

    <?php else : ?>

        <?php echo \'<a href="\' . get_permalink() . \'"><img src="\'. get_template_directory_uri() .\'/assets/img/content/bf-default.gif" alt="bf-default" width="136" height="90" /></a>\' ?>

    <?php endif; ?>

    <?php
        echo \'
            <div class="right-content">
            <h3 class="purple"><a href="\' . get_permalink() . \'">\' . $modtitle .\'</a></h3>
            <p class="date">\' . get_the_date() .\'</p>
            <p>\' . $modcontent . \'&hellip; <a href="\' . get_permalink() . \'">More &rsaquo;</a></p>
            </div>

            </article>\';
    ?>

    <?php   

        /* PageNavi at Bottom */
        if (function_exists(\'wp_pagenavi\')){wp_pagenavi();} 

        // FOR PAGINATION -  reassign the $wp_query variable to what is was originally and then reset the query back to start.
        $wp_query = null;
        $wp_query = $temp;  

        endwhile;
    ?>


    <?php
        endif;
        wp_reset_postdata(); // to reset the loop
    ?>
</div><!-- [END] feed-->
以及下自定义帖子的模板parent-category-2 (自定义模板-category-parent-category-2.php):

<div class="feed med no-border" id="the-posts">

    <?php   

     $paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1; //  FOR PAGINATION

        $category = get_the_category();             
            $args = array(
               \'post_type\' => \'custom-post-type\',
               \'posts_per_page\' => 5, 
               \'page\' => $paged, //  FOR PAGINATION
               \'category__in\' => ($cat),
            );

        // FOR PAGINATION - hijack the $wp_query variable temporarily
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query($args);        

        $second_query = new WP_Query( $args );
        if ( $second_query->have_posts() ):
           while( $second_query->have_posts() ) : $second_query->the_post();

           $titlechars = 45; // Character Limit
           $posttitle = get_the_title();
           $modtitle = substr($posttitle, 0, $titlechars);

           $contentchars = 120; // Character Limit
           $postcontent = get_the_excerpt();
           $modcontent = substr($postcontent, 0, $contentchars);

        echo \'<article \';
        echo \' \' . post_class(\'\',false) . \'  \';
        echo \'>\';
    ?>

    <?php

        if( get_field(\'image\') ):

            $attachment_id = get_field(\'image\');
            $size = \'customfeatins\'; // (thumbnail, medium, large, full or custom size)
            $image = wp_get_attachment_image_src( $attachment_id, $size );

        echo \'<a href="\' . get_permalink() . \'"><img src="\' . $image[0] . \'" alt="\' . get_the_title() .\'" width="136" height="90" /></a>\';
    ?>

    <?php else : ?>

        <?php echo \'<a href="\' . get_permalink() . \'"><img src="\'. get_template_directory_uri() .\'/assets/img/content/bf-default.gif" alt="bf-default" width="136" height="90" /></a>\' ?>

    <?php endif; ?>

    <?php
        echo \'
            <div class="right-content">
            <h3><a class="purple" href="\' . get_permalink() . \'">\' . $modtitle .\'</a></h3>
            <p class="date">\' . get_the_date() .\'</p>
            <p>\' . $modcontent . \'&hellip; <a href="\' . get_permalink() . \'">More &rsaquo;</a></p>
            </div>

            </article>\';
    ?>

    <?php   

        /* PageNavi at Bottom */
        if (function_exists(\'wp_pagenavi\')){wp_pagenavi();} 

        // FOR PAGINATION -  reassign the $wp_query variable to what is was originally and then reset the query back to start.
        $wp_query = null;
        $wp_query = $temp;  
    ?>

    <?php                              
        endwhile;
        endif;
        wp_reset_postdata(); // to reset the loop
    ?>

</div><!-- [END] feed -->
我搜索了这里发布的问题和wp论坛,没有找到任何专门针对儿童类别的内容。

任何帮助都将不胜感激!!!

提前谢谢你。

1 个回复
最合适的回答,由SO网友:ByteMyPixel 整理而成

我终于可以让它工作了!方法如下:

我误解了什么(作为一个WP noob和所有…)是吗get_query_var(\'page\') 用于页面,以及get_query_var(\'paged\') 是针对帖子的。http://codex.wordpress.org/Function_Reference/get_query_varfor

所以我换了这两行

$paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1; // FOR PAGINATION\'page\' => $paged, // FOR PAGINATION

用这两条线

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; // FOR PAGINATION\'paged\' => $paged, // FOR PAGINATION

并删除了\'posts_per_page\' => 5, 参数,然后在“设置”>“阅读”下的“管理”中设置帖子限制

然而,这并不是最终的解决方案。分页仍在返回404custom_post_type 类别。最终修复程序是添加此修补程序:http://wordpress.org/support/topic/custom-types-category-pagination-404#post-1913902 我的职能。php文件。

现在,分页在所有帖子类型的类别和子类别上都可以完美地工作。

结束