是否在未将分类分配给自定义发布类型时删除分类辅助程序?

时间:2015-10-30 作者:Marz

我有一个名为“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
有什么想法可以解决这个问题吗?比如,当没有分配分类术语时,有没有办法让它忽略整个%项目%?谢谢

谢谢

1 个回复
最合适的回答,由SO网友:Marz 整理而成

借助Milo提供的有用提示,我能够在没有为自定义帖子类型分配分类法的情况下,通过向上面更新的函数添加以下代码来删除分类法slug:

add_filter( \'request\', \'project_request_filter\' );
function project_request_filter( $request ){
if( array_key_exists( \'project\' , $request )
    && ! get_term_by( \'slug\', $request[\'project\'], \'project\' ) ){
        $request[\'set\'] = $request[\'project\'];
        $request[\'name\'] = $request[\'project\'];
        $request[\'post_type\'] = \'set\';
        unset( $request[\'project\'] );
}
return $request;
}

相关推荐

GET_PAGE_TEMPLATE_SLUG返回错误路径

我目前遇到了一个非常奇怪的问题,我找不到任何理由。我有一个在所有服务器上都能正常工作的主题。现在,我有一个客户对此有问题。问题是函数get\\u page\\u template\\u slug()返回主题文件夹名称。例如:主题/模板主页。php它应该只是模板主页。没有主题文件夹名称的php。怎么会这样?希望有人能帮我。