我有一个名为“博客”的cpt,现在我想列出所有为该cpt做出贡献的作者。此外,我还想列出所有按月份分组的博客帖子(来自“blog”cpt),就像wp\\u get\\u archives()一样。
作者。我尝试使用wp\\u list\\u authors,但它只列出常规帖子的作者,而不是cpt的作者。我只想列出“blog”cpt的作者。怎么办?
每月档案。我尝试使用wp\\u list\\u archives(),遵循Bainernet的指示(此处:Get monthly archives for custom post type) 上面写着“monthname年”,看起来很棒。但是,创建的permalinks如下所示:www.example。com/2011/10。。。而那将一事无成!我得到一个404错误。所有“博客”cpt帖子都位于www.example下。com/blog,我希望日期存档url也包括/blog所以也许这只是一个重写的东西,但我确实修复了它点击>只需手动键入www.example。com/blog/2011/10也只能让我进入404错误页面。
希望你能帮助我!
更新1我成功地将URL重写到归档文件中,所以现在它们看起来像这样:www.example。com/blog/2011/10
这是我用于重写的代码:
add_rewrite_tag(\'%blog-date%\',\'([\\d]{4}-[\\d]{2}-[\\d]{2})\');
add_action( \'init\', \'blog_rewrites\' );
function blog_rewrites() {
add_rewrite_rule( "blog/day/([\\d]{4}-[\\d]{2}-[\\d]{2})/?$", \'index.php?post_type=blog&blog-date=$matches[1]\', \'top\' );
}
add_filter( \'query_vars\', \'blog_vars\' );
function blog_vars ( $vars ) {
$vars[] = \'blog-date\';
return $vars;
}
但我仍然得到404错误,虽然。。。
应用下面@Bainternet回复中的代码,我可以让我的作者列表正常运行,但我的每月档案链接上仍然有404。但url似乎是正确的:www.example。com/blogg/2011/10这是我函数中的代码。php当前:
add_filter(\'get_archives_link\', \'blog_archive_links\');
function blog_archive_links($html){
$home_url = home_url();
$home_url_with_blog = $home_url .\'/blogg\';
return str_replace($home_url,$home_url_with_blog,$html);
}
//to match http://www.example.com/blog/2011/12/1/ use
add_rewrite_rule(\'^blogg/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$\',\'index.php?post_type=blogg&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]\',\'top\');
//to match http://www.example.com/blog/2011/12/1/page/2 use
add_rewrite_rule(\'^blogg/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$\',\'index.php?post_type=blogg&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&page=$matches[4]\',\'top\');
//to match http://www.example.com/blog/2011/12/ use
add_rewrite_rule(\'^blogg/([0-9]{4})/([0-9]{1,2})/(?$\',\'index.php?post_type=blogg&year=$matches[1]&monthnum=$matches[2]\',\'top\');
//to match http://www.example.com/blog/2011/12/page/2 use
add_rewrite_rule(\'^blogg/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$\',\'index.php?post_type=blogg&year=$matches[1]&monthnum=$matches[2]&page=$matches[3]\',\'top\');
function Cpt_getarchives_where_filter( $where , $r ) {
$post_type = \'blogg\';
return str_replace( "post_type = \'post\'" , "post_type = \'$post_type\'" , $where );
}
这是我侧边栏中的代码。php:
add_filter( \'getarchives_where\' , \'Cpt_getarchives_where_filter\' , 10 , 2 );
wp_get_archives();
remove_filter(\'getarchives_where\' , \'Cpt_getarchives_where_filter\' , 10 );
正如你所看到的,我的cpt不叫“blog”,它实际上叫“blogg”,这是瑞典语的拼写,但不重要。此外,我没有使用任何插件来让cpt归档工作,我只是创建了一个名为archive blogg的文件。php并使用一个名为“Custom Post permalinks”的插件将“blogg”cpt中的博客帖子的永久链接设置为“/blogg”,该插件似乎已经不存在了。它似乎在其他方面都做得很好,但我可以尝试用函数中的一些代码替换该插件。php。
除此之外,还有什么其他可以测试的东西可以让它工作吗?
最合适的回答,由SO网友:Bainternet 整理而成
这应该是两个不同的问题,但无论如何,
我需要使用wp_list_authors
对于自定义帖子类型,我创建了此函数:
function get_authors_ids_by_post_type($type = null,$args = \'\'){
if ($type === null) return;
global $wpdb;
$defaults = array(
\'orderby\' => \'name\', \'order\' => \'ASC\', \'number\' => \'\',
\'optioncount\' => false, \'exclude_admin\' => true,
\'show_fullname\' => false, \'hide_empty\' => true,
\'feed\' => \'\', \'feed_image\' => \'\', \'feed_type\' => \'\', \'echo\' => true,
\'style\' => \'list\', \'html\' => true
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
$return = \'\';
$query_args = wp_array_slice_assoc( $args, array( \'orderby\', \'order\', \'number\' ) );
$query_args[\'fields\'] = \'ids\';
$authors = get_users( $query_args );
$author_count = array();
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = \'$type\' AND " . get_private_posts_cap_sql( $type ) . " GROUP BY post_author") as $row )
$author_count[$row->post_author] = $row->count;
foreach ( $authors as $author_id ) {
$author = get_userdata( $author_id );
if ( $exclude_admin && \'admin\' == $author->display_name )
continue;
$posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
if ( !$posts && $hide_empty )
continue;
$link = \'\';
if ( $show_fullname && $author->first_name && $author->last_name )
$name = "$author->first_name $author->last_name";
else
$name = $author->display_name;
if ( !$html ) {
$return .= $name . \', \';
continue; // No need to go further to process HTML.
}
if ( \'list\' == $style ) {
$return .= \'<li>\';
}
$link = \'<a href="\' . get_author_posts_url( $author->ID, $author->user_nicename ) . \'" title="\' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . \'">\' . $name . \'</a>\';
if ( !empty( $feed_image ) || !empty( $feed ) ) {
$link .= \' \';
if ( empty( $feed_image ) ) {
$link .= \'(\';
}
$link .= \'<a href="\' . get_author_feed_link( $author->ID ) . \'"\';
$alt = $title = \'\';
if ( !empty( $feed ) ) {
$title = \' title="\' . esc_attr( $feed ) . \'"\';
$alt = \' alt="\' . esc_attr( $feed ) . \'"\';
$name = $feed;
$link .= $title;
}
$link .= \'>\';
if ( !empty( $feed_image ) )
$link .= \'<img src="\' . esc_url( $feed_image ) . \'" style="border: none;"\' . $alt . $title . \' />\';
else
$link .= $name;
$link .= \'</a>\';
if ( empty( $feed_image ) )
$link .= \')\';
}
if ( $optioncount )
$link .= \' (\'. $posts . \')\';
$return .= $link;
$return .= ( \'list\' == $style ) ? \'</li>\' : \', \';
}
$return = rtrim($return, \', \');
if ( !$echo )
return $return;
echo $return;
}
这与
wp_list_authors
但您也可以定义帖子类型。
至于基于日期的归档,您得到的是404,因为您的重写是错误的,主要是因为链接的生成方式,其次是因为您的重写规则使用blog-date
这在查询本身的任何地方都没有定义。
要使所需的永久链接正常工作,您需要使用另一个挂钩函数:
add_filter(\'get_archives_link\', \'blog_archive_links\');
function blog_archive_links($html){
$home_url = home_url();
$home_url_with_blog = $home_url .\'blog/\';
return str_replace($home_url,$home_url_with_blog,$html);
}
这应该设置链接,这样剩下的就是让它们工作:)
因此,您的重写规则应该如下所示:
//to match http://www.example.com/blog/2011/12/1/ use
add_rewrite_rule(\'^blog/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$\',\'index.php?post_type=blog&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]\',\'top\');
//to match http://www.example.com/blog/2011/12/1/page/2 use
add_rewrite_rule(\'^blog/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$\',\'index.php?post_type=blog&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&page=$matches[4]\',\'top\');
//to match http://www.example.com/blog/2011/12/ use
add_rewrite_rule(\'^blog/([0-9]{4})/([0-9]{1,2})/(?$\',\'index.php?post_type=blog&year=$matches[1]&monthnum=$matches[2]\',\'top\');
//to match http://www.example.com/blog/2011/12/page/2 use
add_rewrite_rule(\'^blog/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$\',\'index.php?post_type=blog&year=$matches[1]&monthnum=$matches[2]&page=$matches[3]\',\'top\');