wp_dashboard_recent_posts

函式
wp_dashboard_recent_posts ( $args )
引數
  • (array) $args { An array of query and display arguments. @type int $max Number of posts to display. @type string $status Post status. @type string $order Designates ascending ('ASC') or descending ('DESC') order. @type string $title Section title. @type string $id The container id. }
    Required:
返回值
  • (bool) False if no posts were found. True otherwise.
定義位置
相關方法
wp_dashboard_recent_commentswp_dashboard_recent_drafts_wp_dashboard_recent_comments_rowwp_dashboard_events_newswp_dashboard_recent_comments_control
引入
3.8.0
棄用
-

wp_dashboard_recent_posts:此鉤子用於將內容新增到WordPress中的“最近的文章”儀表盤小工具。開發人員可以使用此鉤子向“最近的文章”小工具新增自定義內容,例如與站點上最近的文章相關的附加資訊或操作。

生成即將釋出和最近釋出部分。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_dashboard_recent_posts( $args ) {
$query_args = array(
'post_type' => 'post',
'post_status' => $args['status'],
'orderby' => 'date',
'order' => $args['order'],
'posts_per_page' => (int) $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 = current_time( 'Y-m-d' );
$tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' );
$year = current_time( 'Y' );
while ( $posts->have_posts() ) {
$posts->the_post();
$time = get_the_time( 'U' );
if ( gmdate( 'Y-m-d', $time ) === $today ) {
$relative = __( 'Today' );
} elseif ( gmdate( 'Y-m-d', $time ) === $tomorrow ) {
$relative = __( 'Tomorrow' );
} elseif ( gmdate( 'Y', $time ) !== $year ) {
/* translators: Date and time format for recent posts on the dashboard, from a different calendar year, see https://www.php.net/manual/datetime.format.php */
$relative = date_i18n( __( 'M jS Y' ), $time );
} else {
/* translators: Date and time format for recent posts on the dashboard, see https://www.php.net/manual/datetime.format.php */
$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;
}
function wp_dashboard_recent_posts( $args ) { $query_args = array( 'post_type' => 'post', 'post_status' => $args['status'], 'orderby' => 'date', 'order' => $args['order'], 'posts_per_page' => (int) $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 = current_time( 'Y-m-d' ); $tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' ); $year = current_time( 'Y' ); while ( $posts->have_posts() ) { $posts->the_post(); $time = get_the_time( 'U' ); if ( gmdate( 'Y-m-d', $time ) === $today ) { $relative = __( 'Today' ); } elseif ( gmdate( 'Y-m-d', $time ) === $tomorrow ) { $relative = __( 'Tomorrow' ); } elseif ( gmdate( 'Y', $time ) !== $year ) { /* translators: Date and time format for recent posts on the dashboard, from a different calendar year, see https://www.php.net/manual/datetime.format.php */ $relative = date_i18n( __( 'M jS Y' ), $time ); } else { /* translators: Date and time format for recent posts on the dashboard, see https://www.php.net/manual/datetime.format.php */ $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; }
function wp_dashboard_recent_posts( $args ) {
	$query_args = array(
		'post_type'      => 'post',
		'post_status'    => $args['status'],
		'orderby'        => 'date',
		'order'          => $args['order'],
		'posts_per_page' => (int) $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    = current_time( 'Y-m-d' );
		$tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' );
		$year     = current_time( 'Y' );

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

			$time = get_the_time( 'U' );

			if ( gmdate( 'Y-m-d', $time ) === $today ) {
				$relative = __( 'Today' );
			} elseif ( gmdate( 'Y-m-d', $time ) === $tomorrow ) {
				$relative = __( 'Tomorrow' );
			} elseif ( gmdate( 'Y', $time ) !== $year ) {
				/* translators: Date and time format for recent posts on the dashboard, from a different calendar year, see https://www.php.net/manual/datetime.format.php */
				$relative = date_i18n( __( 'M jS Y' ), $time );
			} else {
				/* translators: Date and time format for recent posts on the dashboard, see https://www.php.net/manual/datetime.format.php */
				$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;
}

常見問題

FAQs
檢視更多 >