wp_dashboard_recent_posts() - WordPress 函数参考中文文档

返回介绍

wp_dashboard_recent_posts()

发布于 2017-09-11 字数 4878 浏览 1041 评论 0

wp_dashboard_recent_posts( array $args )

Generates Publishing Soon and Recently Published sections.


描述


参数

$args

(array)
(Required)
An array of query and display arguments.

  • ‘max’
    (int) Number of posts to display.
  • ‘status’
    (string) Post status.
  • ‘order’
    (string) Designates ascending (‘ASC’) or descending (‘DESC’) order.
  • ‘title’
    (string) Section title.
  • ‘id’
    (string) The container id.

返回值

(bool) False if no posts were found. True otherwise.


源代码

File: wp-admin/includes/dashboard.php

function wp_dashboard_recent_posts( $args ) {
	$query_args = array(
		'post_type'      => 'post',
		'post_status'    => $args['status'],
		'orderby'        => 'date',
		'order'          => $args['order'],
		'posts_per_page' => intval( $args['max'] ),
		'no_found_rows'  => true,
		'cache_results'  => false,
		'perm'           => ( 'future' === $args['status'] ) ? 'editable' : 'readable',
	);

	/**
	 * Filters the query arguments used for the Recent Posts widget.
	 *
	 * @since 4.2.0
	 *
	 * @param array $query_args The arguments passed to WP_Query to produce the list of posts.
	 */
	$query_args = apply_filters( 'dashboard_recent_posts_query_args', $query_args );
	$posts = new WP_Query( $query_args );

	if ( $posts->have_posts() ) {

		echo '<div id="' . $args['id'] . '" class="activity-block">';

		echo '<h3>' . $args['title'] . '</h3>';

		echo '<ul>';

		$today    = date( 'Y-m-d', current_time( 'timestamp' ) );
		$tomorrow = date( 'Y-m-d', strtotime( '+1 day', current_time( 'timestamp' ) ) );

		while ( $posts->have_posts() ) {
			$posts->the_post();

			$time = get_the_time( 'U' );
			if ( date( 'Y-m-d', $time ) == $today ) {
				$relative = __( 'Today' );
			} elseif ( date( 'Y-m-d', $time ) == $tomorrow ) {
				$relative = __( 'Tomorrow' );
			} elseif ( date( 'Y', $time ) !== date( 'Y', current_time( 'timestamp' ) ) ) {
				/* translators: date and time format for recent posts on the dashboard, from a different calendar year, see https://secure.php.net/date */
				$relative = date_i18n( __( 'M jS Y' ), $time );
			} else {
				/* translators: date and time format for recent posts on the dashboard, see https://secure.php.net/date */
				$relative = date_i18n( __( 'M jS' ), $time );
			}

			// Use the post edit link for those who can edit, the permalink otherwise.
			$recent_post_link = current_user_can( 'edit_post', get_the_ID() ) ? get_edit_post_link() : get_permalink();

			$draft_or_post_title = _draft_or_post_title();
			printf(
				'<li><span>%1$s</span> <a href="%2$s" aria-label="%3$s">%4$s</a></li>',
				/* translators: 1: relative date, 2: time */
				sprintf( _x( '%1$s, %2$s', 'dashboard' ), $relative, get_the_time() ),
				$recent_post_link,
				/* translators: %s: post title */
				esc_attr( sprintf( __( 'Edit “%s”' ), $draft_or_post_title ) ),
				$draft_or_post_title
			);
		}

		echo '</ul>';
		echo '</div>';

	} else {
		return false;
	}

	wp_reset_postdata();

	return true;
}

更新日志

Version 描述
3.8.0 Introduced.

相关函数

Uses

  • wp-admin/includes/dashboard.php:
    dashboard_recent_posts_query_args
  • wp-admin/includes/template.php:
    _draft_or_post_title()
  • wp-includes/capabilities.php:
    current_user_can()
  • wp-includes/l10n.php:
    __()
  • wp-includes/l10n.php:
    _x()
  • wp-includes/formatting.php:
    esc_attr()
  • wp-includes/general-template.php:
    get_the_time()
  • wp-includes/class-wp-query.php:
    WP_Query::__construct()
  • wp-includes/query.php:
    wp_reset_postdata()
  • wp-includes/functions.php:
    date_i18n()
  • wp-includes/functions.php:
    current_time()
  • wp-includes/link-template.php:
    get_edit_post_link()
  • wp-includes/link-template.php:
    get_permalink()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/post-template.php:
    get_the_ID()
  • Show 10 more uses
    Hide more uses


Used By

  • wp-admin/includes/dashboard.php:
    wp_dashboard_site_activity()

User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!