is_multi_author()
云策文档标注
概述
is_multi_author() 是一个 WordPress 条件函数,用于判断网站是否有多个作者发布了文章。它通过查询数据库检查已发布文章的作者数量,并返回布尔值。
关键要点
- 函数返回布尔值,表示网站是否有多个作者发布了文章。
- 使用 get_transient() 缓存查询结果以提高性能,避免重复数据库查询。
- 通过 wpdb::get_col() 查询 posts 表中已发布文章的不同作者 ID。
- 支持 apply_filters('is_multi_author', $is_multi_author) 钩子,允许开发者过滤结果。
- 常用于主题开发中,例如根据作者数量添加 CSS 类。
代码示例
// 示例:在 body_class 过滤器中添加 group-blog 类
function wpdocs_body_classes( $classes ) {
if ( is_multi_author() ) {
$classes[] = 'group-blog';
}
return $classes;
}
add_filter( 'body_class', 'wpdocs_body_classes' );
原文内容
Determines whether this site has more than one author.
Description
Checks to see if more than one author has published posts.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Source
function is_multi_author() {
global $wpdb;
$is_multi_author = get_transient( 'is_multi_author' );
if ( false === $is_multi_author ) {
$rows = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" );
$is_multi_author = 1 < count( $rows ) ? 1 : 0;
set_transient( 'is_multi_author', $is_multi_author );
}
/**
* Filters whether the site has more than one author with published posts.
*
* @since 3.2.0
*
* @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
*/
return apply_filters( 'is_multi_author', (bool) $is_multi_author );
}
Hooks
- apply_filters( ‘is_multi_author’, bool $is_multi_author )
-
Filters whether the site has more than one author with published posts.
Changelog
| Version | Description |
|---|---|
| 3.2.0 | Introduced. |
Skip to note 2 content
Codex
Adds a class of `group-blog` in body tag to blogs with more than 1 published author.
function wpdocs_body_classes( $classes ) { if ( is_multi_author() ) { $classes[] = 'group-blog'; } return $classes; } add_filter( 'body_class', 'wpdocs_body_classes' );