自定义帖子类型 Slug 重写问题
Posted
技术标签:
【中文标题】自定义帖子类型 Slug 重写问题【英文标题】:Custom Post Type Slug Rewrite Issues 【发布时间】:2014-11-10 13:59:54 【问题描述】:长话短说,我正在尝试让自定义帖子类型类别显示“项目项目”的位置,如您所见HERE
我认为这与我在此代码中注册自定义帖子类型的方式有关:
<?php
add_action( 'init', 'register_posts' );
function register_posts()
register_post_type( 'team_post',
array(
'labels' => array(
'name' => __( "Team" ,"um_lang"),
'singular_name' => __( "Team" ,"um_lang")
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE),
'supports' => array('title','editor','thumbnail','page-attributes')
)
);
register_post_type( 'project_post',
array(
'labels' => array(
'name' => __( "Projects","um_lang"),
'singular_name' => __( "Project" ,"um_lang")
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE),
'supports' => array('title','editor','thumbnail','page-attributes')
)
);
register_taxonomy('project_category',array (
0 => 'project_post',
),array( 'hierarchical' => true, 'label' => __('Projects Category',"um_lang"),'show_ui' => true,'query_var' => true,'singular_label' => __('Projects Category',"um_lang")) );
?>
【问题讨论】:
最近,我遇到了这个问题。 已解决! [#188834][1] [1]:wordpress.stackexchange.com/questions/94817/… 【参考方案1】:试试这个:
global $wp_rewrite;
$movies_structure = '/movies/%year%/%monthnum%/%day%/%movies%';
$wp_rewrite->add_rewrite_tag("%movies%", '([^/]+)', "movies=");
$wp_rewrite->add_permastruct('movies', $movies_structure, false);
返回链接:
domain.com/movies/movie_name
【讨论】:
【参考方案2】:我想通了。我安装了这个插件:https://wordpress.org/plugins/custom-post-type-permalinks/
然后我没有使用 %categories%(这不起作用),而是使用了特定的自定义帖子类型类别名称(即 %project_category%)
【讨论】:
试试我上面的解决方案,用“project_category”替换“project_item”。所以'rewrite' => array('slug' => "%project_category%", 'with_front' => TRUE) 我试过了,但它只是将 URL 显示为 exampleurl/%projectcategory%/post-name【参考方案3】:这告诉 wordpress 将文本 project_item 插入到 slug 中,这就是正在发生的事情:
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE)
这表示要插入 project_item 的值:
'rewrite' => array('slug' => "%project_item%", 'with_front' => TRUE)
【讨论】:
谢谢你,我试过了,结果出现服务器错误。以上是关于自定义帖子类型 Slug 重写问题的主要内容,如果未能解决你的问题,请参考以下文章