自定义帖子类型分页问题-第1页和第2页显示相同的内容

时间:2018-05-01 作者:doubleedesign

我有一个自定义的帖子类型,用于常见问题,称为问题。其存档位于/faq/.

存档的第1页和第2页当前显示相同的内容。我做了一些挖掘,在第一页,is_paged() 返回为false,并且$wp_query->query_vars[\'paged\'] 返回为0, 而在第2页,它们返回为true2 分别(如预期)。

我有一个名为archive-question.php, 但即使使用默认值,也会出现此问题index.php. 没有自定义查询,只是尝试使用默认的存档功能。

站点上使用的其他CPT没有此问题。

任何关于是什么原因的建议都会有很大的帮助!

索引的模板代码。php:

<div class="small-12 medium-10 xlarge-8 columns">

    <main class="nested row align-justify">
        <div class="small-12 columns">
            <?php
            if ( have_posts() ) {
                /*
                global $wp_query;
                print_r($wp_query->query_vars[\'paged\']);
                if(is_paged()) {
                    echo "Yep, paged";
                } else {
                    echo "Nope, not paged";
                } */
                // Start the Loop
                while ( have_posts() ) : the_post();
                    get_template_part(\'excerpts/excerpt-list\');
                endwhile;
            } else {
                get_template_part(\'template-parts/nothing-found\');
            } // End have_posts() check.
            ?>
        </div>
    </main>

</div>

<?php // Display navigation to next/previous pages when applicable 
if ( is_paged() ) { ?>
    <nav id="post-nav" class="row align-center align-justify">
        <div class="post-previous columns"><?php next_posts_link( __( \'&larr; Older posts\', \'\' ) ); ?></div>
        <div class="post-next columns"><?php previous_posts_link( __( \'Newer posts &rarr;\', \'\' ) ); ?></div>
    </nav>
<?php } ?>

编辑上面的内容以删除自定义分页功能,因为此代码在没有它的情况下经过测试,但仍然存在,因此为了清晰起见,我删除了它。

以下是注册自定义帖子类型的代码:

function doublee_question() {

$labels = array(
    \'name\'                  => _x( \'Frequently Asked Questions\', \'Post Type General Name\', \'doublee\' ),
    \'singular_name\'         => _x( \'Question\', \'Post Type Singular Name\', \'doublee\' ),
    \'menu_name\'             => __( \'Questions\', \'doublee\' ),
    \'name_admin_bar\'        => __( \'Question\', \'doublee\' ),
    \'archives\'              => __( \'Question Archives\', \'doublee\' ),
    \'attributes\'            => __( \'Question Attributes\', \'doublee\' ),
    \'parent_item_colon\'     => __( \'Parent Question:\', \'doublee\' ),
    \'all_items\'             => __( \'All Questions\', \'doublee\' ),
    \'add_new_item\'          => __( \'Add New Question\', \'doublee\' ),
    \'add_new\'               => __( \'Add New\', \'doublee\' ),
    \'new_item\'              => __( \'New Question\', \'doublee\' ),
    \'edit_item\'             => __( \'Edit Question\', \'doublee\' ),
    \'update_item\'           => __( \'Update Question\', \'doublee\' ),
    \'view_item\'             => __( \'View Question\', \'doublee\' ),
    \'view_items\'            => __( \'View Questions\', \'doublee\' ),
    \'search_items\'          => __( \'Search Question\', \'doublee\' ),
    \'not_found\'             => __( \'Not found\', \'doublee\' ),
    \'not_found_in_trash\'    => __( \'Not found in Trash\', \'doublee\' ),
    \'featured_image\'        => __( \'Featured Image\', \'doublee\' ),
    \'set_featured_image\'    => __( \'Set featured image\', \'doublee\' ),
    \'remove_featured_image\' => __( \'Remove featured image\', \'doublee\' ),
    \'use_featured_image\'    => __( \'Use as featured image\', \'doublee\' ),
    \'insert_into_item\'      => __( \'Insert into question\', \'doublee\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded to this question\', \'doublee\' ),
    \'items_list\'            => __( \'Items list\', \'doublee\' ),
    \'items_list_navigation\' => __( \'Items list navigation\', \'doublee\' ),
    \'filter_items_list\'     => __( \'Filter items list\', \'doublee\' ),
);
$rewrite = array(
    \'slug\'                  => \'question\',
    \'with_front\'            => true,
    \'pages\'                 => true,
    \'feeds\'                 => true,
);
$args = array(
    \'label\'                 => __( \'Question\', \'doublee\' ),
    \'description\'           => __( \'Frequently Asked Questions\', \'doublee\' ),
    \'labels\'                => $labels,
    \'supports\'              => array( \'title\', \'editor\', \'revisions\', \'page-attributes\' ),
    \'taxonomies\'            => array( \'question_category\' ),
    \'hierarchical\'          => false,
    \'public\'                => true,
    \'show_ui\'               => true,
    \'show_in_menu\'          => true,
    \'menu_position\'         => 9,
    \'menu_icon\'             => \'dashicons-search\',
    \'show_in_admin_bar\'     => true,
    \'show_in_nav_menus\'     => true,
    \'can_export\'            => true,
    \'has_archive\'           => \'faq\',
    \'exclude_from_search\'   => false,
    \'publicly_queryable\'    => true,
    \'rewrite\'               => $rewrite,
    \'capability_type\'       => \'post\',
);
register_post_type( \'question\', $args );
}add\\u action(\'init\',double\\u question\',0);

我尝试过的其他调试步骤:*确保没有与归档文件段塞冲突的段塞页面(即/faq)*禁用归档问题。php并确保FAQ使用索引。php排除归档模板作为问题来源*刷新永久链接

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

对CPT注册进行以下更改,然后刷新permalinks,解决了问题:

将重写段塞更改为\'faq\'\'has-archive\' 到true 而不是\'faq\'我这样做了#1,以便我的存档页URL仍然是/faq, 副作用是现在的单一问题faq/question-name 而不是question/question-name 但这对这个网站来说没关系。

结束

相关推荐

Permalinks - Archives

WordPress文档说:WordPress offers you the ability to create a custom URL structure for your permalinks and archives. https://codex.wordpress.org/Settings_Permalinks_Screen 我看到此屏幕将如何为特定帖子/页面创建永久链接,但我没有看到此设置屏幕上关于如何为存档帖子/页面创建链接的任何其他详细信息。有人能澄清一下吗?