wp_dashboard_recent_drafts()
云策文档标注
概述
wp_dashboard_recent_drafts() 是一个 WordPress 函数,用于在仪表板显示当前用户的最近草稿。它通过 WP_Query 查询草稿文章,并输出 HTML 列表,支持自定义查询参数和国际化。
关键要点
- 函数功能:显示用户最近草稿,默认查询当前用户的 4 篇草稿文章,按修改时间降序排列。
- 参数:可选 $drafts 参数,为 WP_Post 数组或 false,默认 false 时自动查询。
- 查询参数:使用 get_posts() 查询,可通过 dashboard_recent_drafts_query_args 过滤器自定义查询参数。
- 输出内容:输出 HTML 列表,包括草稿标题、编辑链接、时间和内容预览,支持“查看所有草稿”链接。
- 相关函数:涉及 _draft_or_post_title()、wp_trim_words()、get_the_time() 等辅助函数。
代码示例
function wp_dashboard_recent_drafts( $drafts = false ) {
if ( ! $drafts ) {
$query_args = array(
'post_type' => 'post',
'post_status' => 'draft',
'author' => get_current_user_id(),
'posts_per_page' => 4,
'orderby' => 'modified',
'order' => 'DESC',
);
$query_args = apply_filters( 'dashboard_recent_drafts_query_args', $query_args );
$drafts = get_posts( $query_args );
if ( ! $drafts ) {
return;
}
}
// 输出 HTML 内容
}注意事项
- 函数输出 HTML 内容,直接用于仪表板小部件,需确保在正确上下文中调用。
- 使用 esc_url()、esc_attr()、esc_html() 进行转义,确保安全性。
- 草稿预览长度通过 _x('10', 'draft_length') 定义,可翻译和调整。
原文内容
Show recent drafts of the user on the dashboard.
Parameters
$draftsWP_Post[]|falseoptional-
Array of posts to display.
Default:
false
Source
function wp_dashboard_recent_drafts( $drafts = false ) {
if ( ! $drafts ) {
$query_args = array(
'post_type' => 'post',
'post_status' => 'draft',
'author' => get_current_user_id(),
'posts_per_page' => 4,
'orderby' => 'modified',
'order' => 'DESC',
);
/**
* Filters the post query arguments for the 'Recent Drafts' dashboard widget.
*
* @since 4.4.0
*
* @param array $query_args The query arguments for the 'Recent Drafts' dashboard widget.
*/
$query_args = apply_filters( 'dashboard_recent_drafts_query_args', $query_args );
$drafts = get_posts( $query_args );
if ( ! $drafts ) {
return;
}
}
echo '<div class="drafts">';
if ( count( $drafts ) > 3 ) {
printf(
'<p class="view-all"><a href="%s">%s</a></p>' . "n",
esc_url( admin_url( 'edit.php?post_status=draft' ) ),
__( 'View all drafts' )
);
}
echo '<h2 class="hide-if-no-js">' . __( 'Your Recent Drafts' ) . "</h2>n";
echo '<ul>';
/* translators: Maximum number of words used in a preview of a draft on the dashboard. */
$draft_length = (int) _x( '10', 'draft_length' );
$drafts = array_slice( $drafts, 0, 3 );
foreach ( $drafts as $draft ) {
$url = get_edit_post_link( $draft->ID );
$title = _draft_or_post_title( $draft->ID );
echo "<li>n";
printf(
'<div class="draft-title"><a href="%s" aria-label="%s">%s</a><time datetime="%s">%s</time></div>',
esc_url( $url ),
/* translators: %s: Post title. */
esc_attr( sprintf( __( 'Edit “%s”' ), $title ) ),
esc_html( $title ),
get_the_time( 'c', $draft ),
get_the_time( __( 'F j, Y' ), $draft )
);
$the_content = wp_trim_words( $draft->post_content, $draft_length );
if ( $the_content ) {
echo '<p>' . $the_content . '</p>';
}
echo "</li>n";
}
echo "</ul>n";
echo '</div>';
}
Hooks
- apply_filters( ‘dashboard_recent_drafts_query_args’, array $query_args )
-
Filters the post query arguments for the ‘Recent Drafts’ dashboard widget.
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |