admin管理员组文章数量:1122846
I've followed the code based off this and everything is working perfectly -How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
So I am able to browse taxonomy archives with
my/category
(or)
my/category/subcategory
and view a custom post associated with each term as
my/category/subcategory/custompost
However, I cannot view
my/category/page/2/
or
my/category/subcategory/page/2/
It gives me a 404.
Here is the code I am using.
posttype.my.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');
taxonomy.my.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,
)
)
);
}
main.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
}
If anyone can help or point me in the right direction that'd be amazing. I don't want to resort to separate URL slugs for taxonomies and custom post types :\ Do I have to modify the rewrite rule to include something involving "paged" ?
Thanks
I've followed the code based off this and everything is working perfectly -How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
So I am able to browse taxonomy archives with
my/category
(or)
my/category/subcategory
and view a custom post associated with each term as
my/category/subcategory/custompost
However, I cannot view
my/category/page/2/
or
my/category/subcategory/page/2/
It gives me a 404.
Here is the code I am using.
posttype.my.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');
taxonomy.my.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,
)
)
);
}
main.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
}
If anyone can help or point me in the right direction that'd be amazing. I don't want to resort to separate URL slugs for taxonomies and custom post types :\ Do I have to modify the rewrite rule to include something involving "paged" ?
Thanks
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Sep 30, 2014 at 14:11 tim danielstim daniels 418 bronze badges 3 |2 Answers
Reset to default 0Maybe echoing the last query will help debug the problem.
Paste this on your 404 page:
global $wpdb;
echo "<pre>";
var_dump($wpdb->last_query);
die();
I ran into a problem with pagination for WordPress custom post types a few months ago. What really helped me was this article on Tuts+. The basic idea is to chain your custom page template (page-my.php) to the archive page template (archive-my.php). This enables pagination for Custom Posttypes, together with a small custom pagination function.
After following the steps in the article I no longer got the dreaded 404 when navigating to page 2 and beyond.
You can see a working example here
本文标签: Custom post typetaxonomy rewrite archive page 2 gives 404
版权声明:本文标题:Custom post typetaxonomy rewrite archive page 2 gives 404 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736280818a1926122.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
my/category/page/2/
will try to load amy
post named2
, so yes, you need additional rewrite rules to handle URLs containingpage
. try using this rewrite analyzer plugin to give you a look at how your URLs are being parsed. – Milo Commented Sep 30, 2014 at 15:40$newRules['my/(.+)/page/?([0-9]{1,})/?$'] = 'index.php?mycategory=$matches[1]&paged=$matches[2]';
Seems to have done the trick! – tim daniels Commented Oct 1, 2014 at 9:48