我创建了一个自定义的帖子类型,具有简单的永久链接结构。现在我想用一些额外的信息来增强这个permalink结构。
public function custom_post_mission() {
$labels = array(
...
);
$args = array(
\'labels\' => $labels,
\'description\' => __( \'Holds our missions and specific data\', TEXT_DOMAIN ),
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'author\', \'editor\' ),
\'has_archive\' => true,
\'menu_icon\' => plugin_dir_url( __FILE__ ).\'img/mission.png\',
\'register_meta_box_cb\' => array( $this, \'add_custom_box\' )
);
register_post_type( \'mission\', $args );
}
通过上述编码,我得到了以下permalink结构:
http://localhost/wordpress/mission/<post-title>/
我想将此结构改进为以下结构:http://localhost/wordpress/mission/<year>/<month>/<post-title>/
我该怎么做?如果有办法将旧的permalink结构转移到新的permalink结构,那就太好了,但这不是必需的。
BR&;谢谢
麦贝克
/E: 使用ravi提供的代码示例增强了我的代码
public function custom_post_mission() {
global $wp_rewrite;
$labels = array(
...
);
$args = array(
\'labels\' => $labels,
\'description\' => __( \'Holds our missions and specific data\', TEXT_DOMAIN ),
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'author\', \'editor\' ),
\'has_archive\' => true,
\'menu_icon\' => plugin_dir_url( __FILE__ ).\'img/mission.png\',
\'register_meta_box_cb\' => array( $this, \'add_custom_box\' )
);
register_post_type( \'mission\', $args );
$wp_rewrite->add_permastruct(\'mission\', \'mission/%year%/%monthnum%/%mission%/\', true, 1);
add_rewrite_rule(\'mission/([0-9]{4})/(.+)/?$\', \'index.php?mission=$matches[2]\', \'top\');
$wp_rewrite->flush_rules(); // !!!
}
这将创建以下永久链接结构:
http://localhost/wordpress/mission/%year%/%monthnum%/<mission-title>/
但是
%year%
和
%monthnum%
占位符无法替换为相应的值。
/E2:我终于开始工作了。我需要一个过滤器来解析占位符:
add_filter(\'post_type_link\', \'mission_permalink\', 10, 3);
function mission_permalink( $permalink, $post_id, $leavename ) {
$post = get_post($post_id);
$rewritecode = array(
\'%year%\',
\'%monthnum%\',
\'%day%\',
\'%hour%\',
\'%minute%\',
\'%second%\',
$leavename? \'\' : \'%postname%\',
\'%post_id%\',
\'%category%\',
\'%author%\',
$leavename? \'\' : \'%pagename%\',
);
if ( \'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')) ) {
$unixtime = strtotime($post->post_date);
$category = \'\';
if ( strpos($permalink, \'%category%\') !== false ) {
$cats = get_the_category($post->ID);
if ( $cats ) {
usort($cats, \'_usort_terms_by_ID\'); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent )
$category = get_category_parents($parent, false, \'/\', true) . $category;
}
// show default category in permalinks, without
// having to assign it explicitly
if ( empty($category) ) {
$default_category = get_category( get_option( \'default_category\' ) );
$category = is_wp_error( $default_category ) ? \'\' : $default_category->slug;
}
}
$author = \'\';
if ( strpos($permalink, \'%author%\') !== false ) {
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
}
$date = explode(" ",date(\'Y m d H i s\', $unixtime));
$rewritereplace =
array(
$date[0],
$date[1],
$date[2],
$date[3],
$date[4],
$date[5],
$post->post_name,
$post->ID,
$category,
$author,
$post->post_name,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
// wp_die($permalink );
} else { // if they\'re not using the fancy permalink option
}
return $permalink;
}
我发现add\\u rewrite\\u规则regexp中有一个小错误。正确的一个:
add_rewrite_rule(\'mission/([0-9]{4})/([0-9]{2})/(.+)/?$\', \'index.php?mission=$matches[3]\', \'top\');