【问题标题】:Pagenavi Plugin and Custom Post Type Navigation Error (404)Pagenavi 插件和自定义帖子类型导航错误 (404)
【发布时间】:2011-03-03 12:29:27
【问题描述】:

我也有类似的问题 (https://wordpress.stackexchange.com/questions/9593/custom-post-type-archive-with-pagination),不知道该怎么办!

我有一个自定义帖子类型名称“画廊”,并且我创建了一个模板来显示所有“画廊”项目。网址是 www.domain.com/gallery。我使用 WP_Pagenavi 插件。每当我尝试转到第 2 页或更高页面时,url 变为 www.domain.com/gallery/page/2 并返回 404 页面。我到处读到它,我猜它与重写规则、查询和其他任何东西有关!

我已经尝试添加

add_rewrite_rule( 'gallery/page/([0-9]+)/?$', 'index.php?pagename=gallery&paged=$matches[1]', 'top' );

问题是我不想更改我现在是 /%postname%/ 的永久链接结构。

这是我的完整代码

        <?php

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'post_type' => 'gallery',
            'paged' => $paged,
            'showposts' =>24
            ); query_posts($args);

        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


                ...stuff here...

                <?php endwhile; endif; ?>


        <?php 
            wp_pagenavi( );
        ?>

这是我的库的functions.php代码

add_action('init', gallery);
function gallery() {
$args = array(
        'label' => __('Gallery'),
        'singular_label' => __(Gallery),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')        
);

register_post_type( 'gallery' , $args );
add_rewrite_rule( 'gallery/page/([0-9]+)/?$', 'index.php?pagename=gallery&paged=$matches[1]', 'top' );
}

我正在用这个(grrrrr)拉我的头发。 提前谢谢你!

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您的问题是永久链接结构。 /%postname%/ 是一个糟糕的结构,因为它如何访问数据库。如果您没有在永久链接结构中包含 /%category%/,您将始终遇到自定义帖子类型和自定义分类法的问题。我只是总是使用 /%year%/%category%/%postname%/

    如果您坚持使用上述永久链接结构,那么这应该可以解决您的问题。

    add_rewrite_rule('projects/page/([0-9]+)/?$', 'index.php?pagename=projects&paged=$matches[1]', 'top');
    

    【讨论】:

    • 谢谢!一切都是为了刷新永久链接结构
    • 是的,每当您添加新的分类或 CPT 时,您都需要通过访问页面并单击保存来刷新规则(实际上只需访问页面即可)
    猜你喜欢
    • 1970-01-01
    • 2015-11-13
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 2015-06-07
    • 2014-09-29
    相关资源
    最近更新 更多