wp_latest_comments_draft_or_post_title()WP 3.3.0

Get the post title.

The post title is fetched and if it is blank then a default string is returned.

Copied from wp-admin/includes/template.php, but we can't include that file because:

  1. It causes bugs with test fixture generation and strange Docker 255 error
    codes.
  2. It's in the admin; ideally we shouldn't be including files from the
    admin for a block's output. It's a very small/simple function as well,
    so duplicating it isn't too terrible.

No Hooks.

Return

String. The post title if set; "(no title)" if no title is set.

Usage

wp_latest_comments_draft_or_post_title( $post );
$post(int|WP_Post)
Post ID or WP_Post object.
Default: global $post

Changelog

Since 3.3.0 Introduced.

wp_latest_comments_draft_or_post_title() code WP 6.5.2

function wp_latest_comments_draft_or_post_title( $post = 0 ) {
	$title = get_the_title( $post );
	if ( empty( $title ) ) {
		$title = __( '(no title)' );
	}
	return $title;
}