显示带有作者的帖子,在URL中显示自定义帖子类型

时间:2012-11-19 作者:hlotvonen

我真的很难受。我有一个多用户画廊网站。每个作者都有自己的页面,其中列出了作者的帖子(这是一种自定义帖子类型)。

我这样做是为了让作者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;
}

1 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

您可以使用%author% 中的标记rewrite 中的属性register_post_type(). 然而,尽管重写规则已经添加(刷新之后),WordPress在生成帖子类型的永久链接时并没有用相应的值替换标记。例如,您最终得到了permalink www.example。com/%作者%/库名称

以下内容替换%author% 具有适当的值:

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;
}

结束

相关推荐

Add gravatar to author list

我使用此代码在侧栏中列出网站上的所有作者。它可以工作,除了我还需要把他们的Gravatar图像拉进去。它在主页上循环工作<?php echo get_avatar( get_the_author_email(), \'80\' ); ?> 但我有没有办法把它也添加到这个列表中?而且我想不出使用此代码排除“Admin”帐户的方法,这可能吗?非常感谢。<?php $order = \'user_nicename\'; $user_ids = $wpdb->ge