我花了大约6天的时间,试图研究和破译一种基于高级自定义字段(ACF)选择元素的自定义帖子类型永久链接更改的方法。从我的实验来看,这是成功的一半。所有的文章都有正确的永久链接,这将导致正确的页面设置。遗憾的是,这些自定义页面在访问时似乎只有404页。
有人能告诉我我的代码出了什么问题吗?我已经尽了最大努力做到这一点,但我知道的还不够多,无法最终完成这一系统。
以下是相关的编码段:
register_post_type(
\'articles\', array(
\'labels\' => array(
\'name\' => \'Articles\',
\'singular_name\' => \'Article\',
\'add_new\' => \'Add new article\',
\'edit_item\' => \'Edit article\',
\'new_item\' => \'New article\',
\'view_item\' => \'View article\',
\'search_items\' => \'Search articles\',
\'not_found\' => \'No articles found\',
\'not_found_in_trash\' => \'No articles found in Trash\',
),
\'public\' => true,
\'supports\' => array(
\'title\',
\'editor\',
\'excerpt\',
\'revisions\',
\'thumbnail\'
),
\'rewrite\' => array(\'slug\' => \'community/articles/%category_type%\'),
\'menu_icon\' => "dashicons-groups",
\'capability_type\' => array(\'article\',\'articles\'),
\'map_meta_cap\' => true,
)
);
<小时>
function rem_articles_rewrite( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$ptype=get_post_type($post);
//Check the correct post type
if($ptype=="articles"){
//Here you need to get the value of the field, you have the ID of the post
$post->ID and assign the value to $category variable
$category=get_field(\'category\',$post->ID);
if( $category ){
return str_replace( \'%category_type%\' , $category , $post_link );
}
}
}
return $post_link;
}
add_filter( \'post_type_link\', \'rem_articles_rewrite\', 1, 3 );
预期的URL类似于
https://website.com/community/articles/theory/-ARTICLE_SLUG-/有什么想法我搞砸了什么,或者需要补充什么吗?
提前感谢您的帮助!