我的wordpress网站中需要有3种自定义帖子类型,每种类型都是另一种。
课程>系列>情节
我已经完全创建了一个功能文件,用于注册自定义帖子类型并重写url。一切都很好,除了情节只是返回“找不到页面”。
当前的工作方式是用户创建课程。然后,当他们创建一个系列时,会有一个额外的meta框来选择其子级的课程。然后,他们创建一个情节,在情节页面上,还有一个额外的meta框,用于选择其子级的系列。
有一个功能可以重写页面上的永久链接,以在URL中创建字符串替换标签,然后在保存帖子时,将字符串替换为课程和系列的帖子名称。
<?php
/* ----------------------------------------------------------------------------------------------------------
Register our custom post types
---------------------------------------------------------------------------------------------------------- */
function create_post_type() {
//Courses
register_post_type( 'course',
[
'labels' => array(
'name' => __( 'Courses' ),
'singular_name' => __( 'Course' )
),
'description' => 'All courses',
'public' => true,
'hierarchical' => true,
'rewrite' => array(
'slug' => 'system/courses',
),
'menu_icon' => 'dashicons-welcome-learn-more',
'supports' => ['title', 'custom_fields', 'page-attributes']
]
);
//Series
register_post_type( 'series',
[
'labels' => array(
'name' => __( 'Series' ),
'singular_name' => __( 'Series' )
),
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=course',
'description' => 'Course series',
'hierarchical' => false,
'public' => true,
'supports' => ['title', 'custom_fields']
]
);
//Episodes
register_post_type( 'episodes',
[
'labels' => array(
'name' => __( 'Episodes' ),
'singular_name' => __( 'Episode' )
),
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=course',
'description' => 'Series Episodes',
'hierarchical' => false,
'public' => true,
'supports' => ['title', 'custom_fields']
]
);
}
add_action( 'init', 'create_post_type' );
/* ----------------------------------------------------------------------------------------------------------
Add our meta boxes to our series and episode add/edit page
---------------------------------------------------------------------------------------------------------- */
function my_add_meta_boxes() {
add_meta_box( 'series-parent', 'Course', 'series_attributes_meta_box', 'series', 'side', 'high' );
add_meta_box( 'episodes-parent', 'Series', 'episodes_attributes_meta_box', 'episodes', 'side', 'high' );
}
add_action( 'add_meta_boxes', 'my_add_meta_boxes' );
function series_attributes_meta_box( $post ) {
$post_type_object = get_post_type_object( $post->post_type );
$pages = wp_dropdown_pages( array( 'post_type' => 'course', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __( '(no parent)' ), 'sort_column'=> 'menu_order, post_title', 'echo' => 0 ) );
if ( ! empty( $pages ) ) {
echo $pages;
}
}
function episodes_attributes_meta_box( $post ) {
$post_type_object = get_post_type_object( $post->post_type );
$select = "<select name='parent_id' id='parent_id'>";
$select .= "<option value=''>(No Parent)</option>";
//Get Series
$args = [
'post_type' => 'series',
'posts_per_page' => -1,
'meta_key' => 'series_number',
'orderby' => 'meta_value_num',
'order' => 'ASC'
];
$seriesQuery = new WP_Query($args);
//Loop the series
while($seriesQuery->have_posts()){
$seriesQuery->the_post();
$select .= "<option " . (get_the_ID() == $post->post_parent ? 'selected' : '') . " class='level-0' value='" . get_the_ID() . "'>" . get_the_title(wp_get_post_parent_id(get_the_ID())) . ' - ' . get_the_title() . "</option>";
}
wp_reset_postdata();
$select .= "</select>";
echo $select;
}
/* ----------------------------------------------------------------------------------------------------------
Rewrite permalinks
---------------------------------------------------------------------------------------------------------- */
function my_add_rewrite_rules() {
//Series
add_rewrite_tag('%series%', '([^/]+)', 'series=');
add_permastruct('series', 'system/%course%/%series%', [ 'walk_dirs' => false ]);
add_rewrite_rule('^series/([^/]+)/([^/]+)/([^/]+)/?','index.php?series=$matches[1]','top');
//Episodes
add_rewrite_tag('%episodes%', '([^/]+)', 'episode=');
add_permastruct('episodes', 'system/%course%/%series%/%episodes%', [ 'walk_dirs' => false ]);
add_rewrite_rule('^episodes/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?','index.php?episodes=$matches[1]','top');
}
add_action( 'init', 'my_add_rewrite_rules' );
/* ----------------------------------------------------------------------------------------------------------
String replace on the permalinks before we save them
---------------------------------------------------------------------------------------------------------- */
function series_permalinks($permalink, $post, $leavename) {
$post_id = $post->ID;
if($post->post_type != 'series' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
return $permalink;
}
$parent = $post->post_parent;
$parent_post = get_post( $parent );
$permalink = str_replace('%course%', $parent_post->post_name, $permalink);
return $permalink;
}
add_filter('post_type_link', 'series_permalinks', 10, 3);
function episodes_permalinks($permalink, $post, $leavename) {
$post_id = $post->ID;
if($post->post_type != 'episodes' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
return $permalink;
} else {
$parent_post = get_post( $post->post_parent );
$grandparent_post = get_post( $parent_post->post_parent );
$permalink = str_replace('%course%', $grandparent_post->post_name, $permalink);
$permalink = str_replace('%series%', $parent_post->post_name, $permalink);
return $permalink;
}
}
add_filter('post_type_link', 'episodes_permalinks', 10, 3);
?>
从很多堆栈溢出帖子和在线指南中可以得出很多代码,以使它处于当前位置。
我现在的问题是,每当我创建一个情节时,该情节的永久链接都不起作用(即使在重新刷新我的永久链接之后)。它只会返回“找不到页面”。
我在永久链接重写上做错什么了吗?
注意-必须重写'system / ',我使用的是多主题插件,因此所有内容都必须以'system / '开始。