自定义帖子类型/分类重写归档第2页提供404

时间:2014-09-30 作者:tim daniels

我遵循了基于此的代码,一切都很正常-How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name

因此,我可以使用

my/category
(or)
my/category/subcategory
并查看与每个术语关联的自定义帖子

my/category/subcategory/custompost
但是,我无法查看

my/category/page/2/
or
my/category/subcategory/page/2/
它给我一个404。

这是我正在使用的代码。

posttype。我的php

function create_my_posttype() {
    register_post_type(
        \'my\',
    array(
        \'labels\' => array(
            \'name\' => _x( \'Mys\', \'post type general name\' ),
            \'singular_name\' => _x( \'My\', \'post type singular name\' ),
            \'add_new\' => _x( \'New My\', \'add new singular name\' ),
            \'add_new_item\' => __( \'Add New My\' ),
            \'edit_item\' => __( \'Edit My\' ),
            \'new_item\' => __( \'New My\' ),
            \'view_item\' => __( \'View My\' ),
            \'search_items\' => __( \'Search Mys\' ),
            \'not_found\' =>  __( \'No Mys Found\' ),
            \'not_found_in_trash\' => __( \'No Mys found in Trash\' ),
            \'parent_item_colon\' => \'-:-\'
         ),

        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(
            \'slug\' => \'my/%mycategory%\',
            \'with_front\' => true,
            \'pages\' => true
        ),
        \'capability_type\' => \'post\',
        \'hierarchical\' => true,
        \'show_in_nav_menus\' => false,
        \'menu_position\' => 50,
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'comments\',
            \'shortlink\'
        ),
    )
);
}

add_action( \'init\', \'create_my_posttype\');
分类法。我的php
add_action( \'init\', \'create_my_taxonomies\', 0 );

function create_my_taxonomies() {
    register_taxonomy(
            \'mycategory\',
            \'my\',
            array(
                    \'labels\' => array(
                            \'name\' => \'Mycategories\',
            \'singular_name\' => \'Mycategory\',
            \'search_items\' => \'Search Mycategories\',
            \'all_items\' => \'All Mycategories\',
            \'parent_item\' => \'Parent Mycategories\',
                            \'add_new_item\' => \'Add New Mycategory\',
                            \'new_item_name\' => "New Mycategory",
            \'edit_item\' => \'Edit Mycategory\',
            \'update_item\' => \'Update Mycategory\',
            \'add_new_item\' => \'Add New Mycategory\',
            \'new_item_name\' => \'New MyCategory Name\',
            \'menu_name\' => \'Mycategory\'
                    ),

        \'query_var\' => true,
                    \'show_ui\' => true,
        \'has_archive\' => true,
                    \'show_tagcloud\' => false,
                    \'hierarchical\' => true,
                    \'with_front\' => true,
                    \'rewrite\' => array(
                            \'slug\' => \'my\',
                            \'with_front\' => true,
            \'hierarchical\' => true,
                    )
            )
    );
}

主。php

include( \'posttype.my.php\' );
include( \'taxonomy.my.php\' );


add_filter(\'rewrite_rules_array\', \'mmp_rewrite_rules\');
function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules[\'my/(.+)/(.+)/(.+)/?$\'] = \'index.php?my=$matches[3]\';
$newRules[\'my/(.+)/?$\']                = \'index.php?mycategory=$matches[1]\';

return array_merge($newRules, $rules);
}

function filter_post_type_link($link, $post)
{
if ($post->post_type != \'my\')
    return $link;

if ($cats = get_the_terms($post->ID, \'mycategory\'))
{
    $link = str_replace(\'%mycategory%\', get_taxonomy_parents(array_pop($cats)->term_id, \'mycategory\', false, \'/\', true), $link); // see custom function defined below
}
return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);


function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = \'/\', $nicename = false, $visited = array()) {
$chain = \'\';
$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) {
    return $parent;
}

if ($nicename)
    $name = $parent -> slug;
else
    $name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {
    $visited[] = $parent -> parent;
    $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

}

if ($link) {
    // nothing, can\'t get this working :(
} else
    $chain .= $name . $separator;
return $chain;
}

class main_app {
    function __construct() {
    } // end class main_app


}
如果有人能帮我或给我指出正确的方向,那将是令人惊讶的。我不想对分类法和自定义帖子类型使用单独的URL段塞:\\n我是否必须修改重写规则以包含涉及“分页”的内容?

谢谢

2 个回复
SO网友:Diogo Gomes

也许回显最后一个查询将有助于调试问题
将此粘贴到404页上:

global $wpdb;
echo "<pre>";
var_dump($wpdb->last_query);
die();

SO网友:SdeWijs

几个月前,我遇到了WordPress自定义帖子类型的分页问题。真正帮助我的是this article 在Tuts+。基本思想是将自定义页面模板(page my.php)链接到归档页面模板(archive my.php)。这将启用自定义PostType的分页,以及一个小型的自定义分页函数。

在遵循了本文中的步骤之后,当导航到第2页及以后的页面时,我再也不用担心404了。

您可以看到一个工作示例here

结束

相关推荐

显示Archives.php中的所有自定义帖子类型

我该怎么做?archive.php 只有以下内容:wp_get_archives(\'type=monthly\'); 以及wp_get_archives() 没有显示所有帖子类型的参数。我也认为archive-[post_type].php 不是我要找的,因为我希望所有帖子类型都显示在一个归档页面中。谢谢W