基于主题How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
我的代码是
// register the new post type
register_post_type( \'vehicles_listing\',
array( \'labels\' => array(
\'name\' => __( \'Vehicles\' ),
\'singular_name\' => __( \'Vehicle\' ),
\'add_new\' => __( \'Add New\' ),
\'add_new_item\' => __( \'Create New Vehicle\' ),
\'edit\' => __( \'Edit\' ),
\'edit_item\' => __( \'Edit Vehicle\' ),
\'new_item\' => __( \'New Vehicle\' ),
\'view\' => __( \'View Vehicles\' ),
\'view_item\' => __( \'View Vehicles\' ),
\'search_items\' => __( \'Search Vehicles\' ),
\'not_found\' => __( \'No vehicles found\' ),
\'not_found_in_trash\' => __( \'No vehicles found in trash\' ),
\'parent\' => __( \'Parent vehicle\' ),
),
\'description\' => __( \'This is where you can create new vehicles on your site.\' ),
\'public\' => true,
\'show_ui\' => true,
\'capability_type\' => \'post\',
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'menu_position\' => 2,
\'menu_icon\' => get_stylesheet_directory_uri() . \'/images/tag_orange.png\',
\'hierarchical\' => true,
\'_builtin\' => false, // It\'s a custom post type, not built in!
\'rewrite\' => array( \'slug\' => \'vehicles/%vehicle_cat%\', \'with_front\' => true ),
\'query_var\' => true,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'custom-fields\' ),
)
);
//hook into the init action and call create_book_taxonomies when it fires
add_action( \'init\', \'create_vehicle_taxonomies\', 0 );
add_action(\'admin_init\', \'flush_rewrite_rules\');
//create two taxonomies, genres and writers for the post type "book"
function create_vehicle_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
\'name\' => _x( \'Vehicle Categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Vehicle Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Vehicle Category\' ),
\'all_items\' => __( \'All Vehicle Categories\' ),
\'parent_item\' => __( \'Parent Vehicle Category\' ),
\'parent_item_colon\' => __( \'Parent Vehicle Category:\' ),
\'edit_item\' => __( \'Edit Vehicle Category\' ),
\'update_item\' => __( \'Update Vehicle Category\' ),
\'add_new_item\' => __( \'Add New Vehicle Category\' ),
\'new_item_name\' => __( \'New Vehicle Category Name\' ),
\'menu_name\' => __( \'Vehicle Categories\' ),
);
register_taxonomy(\'vehicle_cat\',array(\'vehicles_listing\'), array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
//\'rewrite\' => true,
\'rewrite\' => array( \'slug\' => \'vehicles\', \'with_front\' => true, \'hierarchical\' => true ),
));
}
add_action(\'vehicle_cat_add_form\', \'qtrans_modifyTermFormFor\');
add_action(\'vehicle_cat_edit_form\', \'qtrans_modifyTermFormFor\');
add_filter(\'rewrite_rules_array\', \'mmp_rewrite_rules\');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules[\'/(.+)/(.+)/?$\'] = \'index.php?custom_post_type_name=$matches[3]\';
$newRules[\'/(.+)/?$\'] = \'index.php?custom_taxonomy=$matches[1]\';
return array_merge($newRules, $rules);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != \'vehicles_listing\')
return $link;
if ($cats = get_the_terms($post->ID, \'vehicle_cat\'))
{
$link = str_replace(\'%vehicle_cat%\', get_taxonomy_parents(array_pop($cats)->term_id, \'vehicle_cat\', false, \'/\', true), $link); // see custom function defined below
}
return preg_replace(\'#/+#\', \'/\', $link);
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
// my own function to do what get_category_parents does for other taxonomies
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;
}
在wp中,admin显示链接“http:/mydomain.com/vehicles/truck/man/a4/”,但它会生成真正的链接,如“
http://mydomain.com/mydomain.com/vehicles/truck/man/ret/“和”http:/mydomain。com/vehicles/truck/man/a4/“给出404。我做错了什么。
当做
PART 2.我在github中有另一个与此解决方案相关的示例(https://gist.github.com/kasparsd/2924900)
///////////////////////////////////////////////////////////////////
add_action(\'init\', \'_init_recipe_post_type\');
function _init_recipe_post_type() {
// Create taxonomy (ingredients i.e. tags)
// -------------------------------------------------------------
register_taxonomy(
\'recipe_ingredient\',
array( \'recipe\' ),
array(
\'labels\' => array(
\'name\' => __( \'Ingredients\' ),
\'singular_name\' => __( \'Ingredient\' ),
\'search_items\' => __( \'Search Ingredients\' ),
\'popular_items\' => __( \'Popular Ingredients\' ),
\'all_items\' => __( \'All Ingredients\' ),
\'edit_item\' => __( \'Edit Ingredient\' ),
\'update_item\' => __( \'Update Ingredient\' ),
\'add_new_item\' => __( \'Add New Ingredient\' ),
\'new_item_name\' => __( \'New Ingredient\' ),
),
\'public\' => true,
\'show_in_nav_menus\' => true,
\'show_ui\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'hierarchical\' => true,
\'query_var\' => true,
// this sets the taxonomy view URL (must have tag base i.e. /with)
// this can be any depth e.g. food/cooking/recipes/with
\'rewrite\' => array( \'slug\' => \'recipes\', \'with_front\' => false ),
)
);
// Create taxonomy (cuisines i.e. categories)
// -------------------------------------------------------------
register_taxonomy(
\'recipe_cuisine\',
array( \'recipe\' ),
array(
\'labels\' => array(
\'name\' => __( \'Cuisines\' ),
\'singular_name\' => __( \'Cuisine\' ),
\'search_items\' => __( \'Search Cuisines\' ),
\'popular_items\' => __( \'Popular Cuisines\' ),
\'all_items\' => __( \'All Cuisines\' ),
\'parent_item\' => __( \'Parent Cuisine\' ),
\'parent_item_colon\' => __( \'Parent Cuisine:\' ),
\'edit_item\' => __( \'Edit Cuisine\' ),
\'update_item\' => __( \'Update Cuisine\' ),
\'add_new_item\' => __( \'Add New Cuisine\' ),
\'new_item_name\' => __( \'New Cuisine\' ),
),
\'public\' => true,
\'show_in_nav_menus\' => true,
\'show_ui\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'hierarchical\' => true,
\'query_var\' => true,
// this sets the taxonomy view URL (must have category base i.e. /type)
// this can be any depth e.g. food/cooking/recipes/type
\'rewrite\' => array( \'slug\' => \'recipes\', \'with_front\' => false ),
)
);
// Create post type (recipes i.e. posts)
// -------------------------------------------------------------
register_post_type( \'recipe\',
array(
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'menu_position\' => 30,
\'labels\' => array(
\'name\' => __( \'Recipes\' ),
\'singular_name\' => __( \'Recipe\' ),
\'add_new\' => __( \'Add New\' ),
\'add_new_item\' => __( \'Add New Recipe\' ),
\'edit\' => __( \'Edit Recipe\' ),
\'edit_item\' => __( \'Edit Recipe\' ),
\'new_item\' => __( \'New Recipe\' ),
\'view\' => __( \'View Recipe\' ),
\'view_item\' => __( \'View Recipe\' ),
\'search_items\' => __( \'Search Recipe\' ),
\'not_found\' => __( \'No Recipes found\' ),
\'not_found_in_trash\' => __( \'No Recipes found in Trash\' )
),
\'has_archive\' => true,
\'supports\' => array(\'title\', \'editor\', \'excerpt\', \'thumbnail\'),
\'query_var\' => true,
// this sets where the Recipes section lives and contains a tag to insert the Cuisine in URL below
// this can be any depth e.g. food/cooking/recipes/%recipe_cuisines%
\'rewrite\' => array( \'slug\' => \'recipes/%recipe_cuisine%\', \'with_front\' => true,\'hierarchical\' => true, ),
)
);
// Make permalinks for Recipes pretty (add Cuisine to URL)
// -------------------------------------------------------------
add_filter(\'post_type_link\', \'recipe_permalink_filter_function\', 1, 3);
function recipe_permalink_filter_function( $post_link, $id = 0, $leavename = FALSE ) {
if ( strpos(\'%recipe_cuisine%\', $post_link) === \'FALSE\' ) {
return $post_link;
}
$post = get_post($id);
if ( !is_object($post) || $post->post_type != \'recipe\' ) {
return $post_link;
}
// this calls the term to be added to the URL
$terms = wp_get_object_terms($post->ID, \'recipe_cuisine\');
if ( !$terms ) {
return str_replace(\'recipes/%recipe_cuisine%/\', \'\', $post_link);
}
return str_replace(\'%recipe_cuisine%\', $terms[0]->slug, $post_link);
}
// -------------------------------------------------------------
// FIX - Makes permalinks work!
// This must come at the end of your LAST custom post type
// REMOVE after development (when everything\'s working!)
// -------------------------------------------------------------
///flush_rewrite_rules();
// -------------------------------------------------------------
}
看起来一切正常,但“错误404-找不到页面”仍然存在。请帮助了解如何解决此问题。
向所有阅读和回复的人致意。