wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_dashboard_on_this_day › WordPress Function

Seit7.1.0
Veraltetn/v
wp_dashboard_on_this_day ( Keine Parameter )
Definiert in:
Codex:

Renders the On This Day dashboard widget.

Outputs the matching posts grouped by publication year, newest year first.


Quellcode

function wp_dashboard_on_this_day() {
	$posts = wp_dashboard_on_this_day_get_posts();

	if ( empty( $posts ) ) {
		// Placeholder shown when a user reveals the hidden widget via Screen
		// Options on a day with no matching posts.
		echo '<p>' . esc_html__( 'No posts were published on this day in previous years.' ) . '</p>';
		return;
	}

	$posts_by_year = array();
	$post_count    = count( $posts );

	foreach ( $posts as $post ) {
		$year = get_the_date( 'Y', $post );

		if ( ! isset( $posts_by_year[ $year ] ) ) {
			$posts_by_year[ $year ] = array();
		}

		$posts_by_year[ $year ][] = $post;
	}

	/* translators: Date format for the On This Day widget date, without year. See https://www.php.net/manual/datetime.format.php */
	$date = '<strong>' . esc_html( wp_date( _x( 'F jS', 'on this day date format' ) ) ) . '</strong>';
	?>
	<div class="wp-on-this-day-widget">
		<p>
			<?php
			if ( 1 === $post_count ) {
				printf(
					/* translators: %s: Date, without year. */
					esc_html__( 'One post has been published on %s:' ),
					$date // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Date is escaped above.
				);
			} else {
				printf(
					esc_html(
						/* translators: 1: Number of posts, 2: Date, without year. */
						_n(
							'%1$s post has been published on %2$s:',
							'%1$s posts have been published on %2$s:',
							$post_count
						)
					),
					esc_html( number_format_i18n( $post_count ) ),
					$date // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Date is escaped above.
				);
			}
			?>
		</p>
		<ul>
			<?php foreach ( $posts_by_year as $year => $year_posts ) : ?>
				<li>
					<h3><?php echo esc_html( $year ); ?></h3>
					<ul>
						<?php foreach ( $year_posts as $year_post ) : ?>
							<?php
							$title = get_the_title( $year_post );

							if ( '' === trim( $title ) ) {
								$title = __( '(no title)' );
							}

							$author_id   = (int) $year_post->post_author;
							$author_name = $author_id > 0 ? (string) get_the_author_meta( 'display_name', $author_id ) : '';
							$show_author = '' !== trim( $author_name ) && get_current_user_id() !== $author_id;
							?>
							<li>
								<a href="<?php echo esc_url( get_permalink( $year_post ) ); ?>"><?php echo esc_html( $title ); ?></a>
								<?php if ( $show_author ) : ?>
									<?php
									echo '<span class="wp-on-this-day-post-author">' . esc_html(
										sprintf(
											/* translators: %s: Post author's display name. */
											__( 'by %s' ),
											$author_name
										)
									) . '</span>';
									?>
								<?php endif; ?>
							</li>
						<?php endforeach; ?>
					</ul>
				</li>
			<?php endforeach; ?>
		</ul>
	</div>
	<?php
}