显示404的URL中的自定义分类

时间:2018-02-28 作者:kevngibsn

使用带有自定义分类法的自定义帖子类型,我需要一个/post-type/custom-taxonomy/article-name的自定义url结构。

我已经搜索并阅读了Permalinks: custom post type -> custom taxonomy -> postHow to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name (还有其他一些,主要是这些)但我仍然有一些我找不到答案的问题。

自定义帖子类型和分类很好,当我点击帖子时,url显示了我想要它的方式,但是我得到的不是我的帖子,而是404错误。我已经进入permalinks,节省了一百万次,但仍然得到了404。

首先,我要注册分类法:

register_taxonomy( \'mediacentral_type\',
    array(\'ssd-mediacentral\'), 
    array(\'hierarchical\' => true,     
        \'labels\' => array(
            \'name\' => __( \'Categories\', \'ssd\' ),
            \'singular_name\' => __( \'Category\', \'ssd\' ),
            \'search_items\' =>  __( \'Search Categories\', \'ssd\' ),
            \'all_items\' => __( \'All Categories\', \'ssd\' ),
            \'parent_item\' => __( \'Parent Category\', \'ssd\' ),
            \'parent_item_colon\' => __( \'Parent Category:\', \'ssd\' ),
            \'edit_item\' => __( \'Edit Categories\', \'ssd\' ),
            \'update_item\' => __( \'Update Category\', \'ssd\' ),
            \'add_new_item\' => __( \'Add New Category\', \'ssd\' ), 
            \'new_item_name\' => __( \'New Post Category\', \'ssd\' ) 
        ),
        \'show_admin_column\' => true,
        \'show_ui\' => true,
        \'public\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'media-central\', \'with_front\' => false ),
    )
);
然后标记,

然后是职位类型:

register_post_type( \'ssd-mediacentral\',
    array( \'labels\' => array(
        \'name\' => __( \'Media Central\', \'ssd\' ), 
        \'singular_name\' => __( \'Post\', \'ssd\' ), 
        \'all_items\' => __( \'All Posts\', \'ssd\' ), 
        \'add_new\' => __( \'Add New Post\', \'ssd\' ), 
        \'add_new_item\' => __( \'Add Post\', \'ssd\' ), 
        \'edit\' => __( \'Edit\', \'ssd\' ), 
        \'edit_item\' => __( \'Edit Post\', \'ssd\' ), 
        \'new_item\' => __( \'New Post\', \'ssd\' ), 
        \'view_item\' => __( \'View Post\', \'ssd\' ), 
        \'search_items\' => __( \'Search Posts\', \'ssd\' ), 
        \'not_found\' =>  __( \'Nothing found in the Database.\', \'ssd\' ),
        \'not_found_in_trash\' => __( \'Nothing found in Trash\', \'ssd\' ), 
        \'parent_item_colon\' => \'\'
        ),
        \'description\' => __( \'SSD Media Central Posts\', \'ssd\' ),
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'menu_position\' => 8, 
        \'menu_icon\' => get_stylesheet_directory_uri() . \'/library/images/custom-post-icon.png\', 
        \'rewrite\'   => array( \'slug\' => \'media-central/%mc_type%\', \'with_front\' => false ), 
        \'has_archive\' => \'media-central\', 
        \'capability_type\' => \'post\',
        \'hierarchical\' => true,
        \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'sticky\')
    )
);
然后过滤器:

function wpa_mc_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == \'ssd-mediacentral\' ){
    $terms = wp_get_object_terms( $post->ID, \'mediacentral_type\' );
    if( $terms ){
        return str_replace( \'%mc_type%\' , $terms[0]->slug , $post_link );
    }
}
return $post_link;
}
add_filter( \'post_type_link\', \'wpa_mc_permalinks\', 1, 2 );
现在,当我转到我的帖子页面/media central并单击名为“测试播客”的帖子时,它会转到URL/media central/播客/测试播客,但我得到的是404。不知道为什么这是显示404,我即将放弃它,任何帮助都将不胜感激。

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

找到了问题的根源。

How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name

我从一个全新的内容类型开始,上面的解决方案中的第一个答案对我很有用。

在试图解决最初的问题时,我仍然有一些重复的url问题,但我能够做一些整理来解决这个问题。

结束

相关推荐

How to replace permalinks

我已将wordpress项目导出并导入live server。现在的问题是,我将permalink结构作为“localhost”格式。当我单击网站中的任何链接时,它会将我重定向到localhost。我怎样才能改变这一点?我的htaccess文件如下所示<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /myproject/ RewriteRule ^index\\.php$ - [L] RewriteCo