WordPress 作者存档页面显示自定义文章类型的内容

WordPress 默认会创建一个作者存档页面,例如 https://www.wpdaxue.com/author/cmhello 会显示该作者发布的所有文章,但是如果你的网站使用了自定义文章类型,默认情况下,在这个存档页面是不显示该作者发布的自定义文章类型的内容的。如果你希望它显示,可以在主题的 functions.php 添加下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * WordPress 作者存档页面显示自定义文章类型的内容
 * https://www.wpdaxue.com/custom-post-types-author-archives.html
 */
function post_types_author_archives($query) {
	// 添加 questions 这个自定义文章类型到作者存档
	if ( $query->is_author )
		$query->set( 'post_type', array('questions', 'post') );	// 运行后移除这个挂载动作,防止无限执行
	remove_action( 'pre_get_posts', 'post_types_author_archives' );
}
add_action( 'pre_get_posts', 'post_types_author_archives' );

/**
* WordPress 作者存档页面显示自定义文章类型的内容
* https://www.wpdaxue.com/custom-post-types-author-archives.html
*/
function post_types_author_archives($query) {
// 添加 questions 这个自定义文章类型到作者存档
if ( $query->is_author )
$query->set( ‘post_type’, array(‘questions’, ‘post’) );
// 运行后移除这个挂载动作,防止无限执行
remove_action( ‘pre_get_posts’, ‘post_types_author_archives’ );
}
add_action( ‘pre_get_posts’, ‘post_types_author_archives’ );

请根据自己的实际,修改第 8 行的文章类型。

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号