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

时间:2019-08-29   阅读:565

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

1
2
3
/** * 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 行的文章类型。

上一篇:WordPress 快速替换文章/评论的某些文字内容

下一篇:给WordPress里包含下载资源的文章,加一个下载标识

网友评论