我有一个名为“set”的自定义帖子类型和一个名为“project”的分类法。此自定义职位类型下的大多数职位将分配一个项目期限,但不一定。当项目期限has not 如果已分配,我最终会得到一个URL,例如:
example.com/sets/no-project/the-post-name
在这种情况下,我希望它像这样显示
when no taxonomy is assigned:
example.com/sets/the-post-name
这是我的原始代码:
add_filter(\'post_link\', \'sets_permalink\', 10, 3);
add_filter(\'post_type_link\', \'sets_permalink\', 10, 3);
function sets_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%project%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'project\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'no-project\';
return str_replace(\'%project%\', $taxonomy_slug, $permalink);
}
在遵循
this post, 我能想到这个:
更新代码,带注册CPT和;分类法(未完全起作用!)
// Register Taxonomy
add_action( \'init\', \'create_project_taxonomies\', 0 );
function create_project_taxonomies() {
$labels = array(
\'name\' => _x( \'Project\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Project\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Projects\' ),
\'all_items\' => __( \'All Projects\' ),
\'parent_item\' => __( \'Parent Project\' ),
\'parent_item_colon\' => __( \'Parent Project:\' ),
\'edit_item\' => __( \'Edit Project\' ),
\'update_item\' => __( \'Update Project\' ),
\'add_new_item\' => __( \'Add New Project\' ),
\'new_item_name\' => __( \'New Project Name\' ),
\'menu_name\' => __( \'Projects\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'_builtin\' => false,
\'public\' => true,
\'show_ui\' => true,
\'with_front\' => false,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'sets\' ),
);
register_taxonomy( \'project\', array(\'set\'), $args );
}
// Project Permalink
add_filter(\'post_type_link\', \'sets_permalink\', 10, 3);
function sets_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%project%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'project\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug . \'/\';
else $taxonomy_slug = \'\';
return str_replace(\'%project%/\', $taxonomy_slug, $permalink);
}
add_action (\'init\', \'custom_rewrite\', 10, 0);
function custom_rewrite () {
add_rewrite_rule( \'sets/([^/]+)/?$\', \'index.php?set=$matches[1]\', \'top\');
flush_rewrite_rules();
}
// Custom Post Type
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
register_post_type( \'set\',
array(
\'labels\' => array(
\'name\' => __( \'Sets\' ),
\'all_items\' => __( \'All Sets\'.$plural),
\'singular_name\' => __( \'Set\' ),
\'edit_item\' => __( \'Edit Set\' ),
),
\'public\' => true,
\'has_archive\' => \'sets\',
\'can_export\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'query_var\' => true,
\'show_in_nav_menus\' => true,
\'show_ui\' => true,
\'menu_position\' => 8,
\'capability_type\' => \'post\',
\'heirarchical\' => true,
\'menu_icon\' => \'dashicons-playlist-audio\',
\'rewrite\' => array(\'slug\' => \'sets/%project%\', \'with_front\' => false ),
\'supports\' => array( \'title\', \'thumbnail\', \'editor\'),
\'register_meta_box_cb\' => \'set_meta_boxes\',
)
);
flush_rewrite_rules();
}
问题出现,更新后的代码正常工作。然而,当我试图查看一篇没有指定分类法的文章时,我遇到了一个奇怪的问题。例如
this URL works but it returns a 404 page:
example.com/sets/the-post-name
似乎上一个URL的实际帖子显示在:
example.com/sets/project/the-post-name
但如上所述,
I need it to display like this when no taxonomy is assigned:
example.com/sets/the-post-name
现在,当我指定一个名为
groove-sessions“对于这篇文章,所有内容都显示得很完美,链接没有显示404页。例如,
this URL works:
example.com/sets/groove-sessions/the-post-name
有什么想法可以解决这个问题吗?比如,当没有分配分类术语时,有没有办法让它忽略整个%项目%?谢谢
谢谢