我花了大量的时间试图找到这个问题的解决方案,阅读了很多文章,并从stackoverflow和其他地方尝试了很多'修复',都没有成功。WordPress的自定义固定链接自定义帖子类型的结构
我正在尝试为Wordpress(版本4.5.3)中创建的自定义帖子类型创建永久链接结构。帖子类型被称为“视频”。默认情况下,我的固定链接看起来像'http://localhost.wordpress/videos/my-video-post'。我想将链接中的“视频”替换为帖子的类别名称,即 'http://localhost.wordpress/computer-lessons/my-video-post',其中类别是“计算机课程”。我已经能够使用下面的代码创建工作永久链接,例如'http://localhost.wordpress/categories/computer-lessons/my-video-post',但我想摆脱链接中的'/ categories /'部分。我目前正在使用的代码是
add_action('init', 'video_post_type');
function video_post_type() {
register_post_type('video', array(
'labels' => array(
'name' => 'Videos',
'singular_name' => 'Video',
),
'rewrite' => array('slug' => 'categories/%category%'),
'taxonomies' => array('post_tag', 'category'),
'description' => 'Video resources.',
'public' => true,
'menu_position' => 20,
'supports' => array('title', 'editor', 'custom-fields')
));
}
function videos_post_link($post_link, $id = 0){
$post = get_post($id);
if (is_object($post)){
$terms = wp_get_object_terms($post->ID, 'category');
if($terms){
return str_replace('%category%' , $terms[0]->slug , $post_link);
}
}
return $post_link;
}
add_filter('post_type_link', 'videos_post_link', 1, 3);
我已经试过这段代码的很多很多的变化,但每当我得到我要找的永久链接结构给出了404
我开始觉得这可能是不可能的,因为我已经阅读了许多与此相关的文章和帖子,并且没有找到可行的解决方案。
非常感谢提前。
感谢您的回复mattkrupnik。不幸的是,我无法检查您的解决方案,因为我已经完成了我正在开发的项目,并且无法再访问代码,但是如果/当我再次遇到此问题时,我一定会尝试您的解决方案。 –