我真的很难受。我有一个多用户画廊网站。每个作者都有自己的页面,其中列出了作者的帖子(这是一种自定义帖子类型)。
我这样做是为了让作者url(author.php)是www.example。com/firstname-lastname/。但当访问者点击作者的帖子时,url会变为www.example。com/custom post type name/postname/,而我希望它是www.example。com/firstname/lastname/postname/。我如何做到这一点?具有htaccess?或者在注册帖子类型时更改重写规则?怎样
我用它在函数中注册我的帖子类型。php文件:
// Make custom post type Add media
function galleryRegister()
{
$labels = array(
\'name\' => _x(\'Add gallery\', \'post type general name\'),
\'singular_name\' => _x(\'Add gallery\', \'post type singular name\'),
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\', \'editor\', \'thumbnail\'),
\'rewrite\' => false,
\'show_in_nav_menus\' => true,
);
register_post_type(\'gallery\' , $args);
}
add_action(\'init\', \'galleryRegister\');
谢谢你的帮助。
编辑:我发现我可以手动键入www.example。com/firstname/lastname/postname/,它显示了正确的帖子,但永久链接仍然是旧的www。实例com/custom post type name/postname/everywhere
Solution:谢谢Stephen的回答!这是我使用的代码,它起到了作用:
// ****************************************************
// Make a custom post type "Add gallery"
// ****************************************************
function galleryRegister()
{
$labels = array(
\'name\' => _x(\'Add gallery\', \'post type general name\'),
\'singular_name\' => _x(\'Add gallery\', \'post type singular name\'),
\'add_new\' => _x(\'Add gallery \', \'portfolio item\'),
\'all_items\' => __( \'Manage your galleries\' ),
\'add_new_item\' => __(\'Add gallery\'),
\'edit_item\' => __(\'Edit your galleries\'),
\'new_item\' => __(\'New gallery\'),
\'view_item\' => __(\'View gallery on site\'),
\'search_items\' => __(\'Search galleries\'),
\'not_found\' => __(\'Nothing found\'),
\'not_found_in_trash\' => __(\'Nothing found in Trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\', \'editor\', \'thumbnail\'),
\'rewrite\' => array( \'slug\' => _x( \'%author%\', \'URL slug\') ),
\'show_in_nav_menus\' => true,
);
register_post_type(\'gallery\' , $args);
}
add_action(\'init\', \'galleryRegister\');
// ****************************************************
// Flush rewrite rules. Delete this
// ****************************************************
function my_rewrite_flush() {
my_cpt_init();
// ATTENTION: This is *only* done during plugin activation hook in this example!
// You should *NEVER EVER* do this on every page load!!
flush_rewrite_rules();
}
register_activation_hook( __FILE__, \'my_rewrite_flush\' );
// ****************************************************
// Make author as slug for posts
// ****************************************************
add_filter(\'post_type_link\', \'wpse73228_author_tag\',10,4);
function wpse73228_author_tag($post_link, $post, $leavename, $sample){
if( \'gallery\' != get_post_type($post) )
return $post_link;
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
$post_link = str_replace(\'%author%\', $author, $post_link);
return $post_link;
}