Underscores Theme Unit Testing - Catching Untitled Posts

admin2025-06-03  13

Wordpress 5.0.3 | Underscores Theme

I'm unit testing a theme I based on underscores using the wptest.io xml.

Underscores does not gracefully catch untitled posts. There is no title in either singular or list form if the post title is blank.

In the theme's content.php, this is what exists:

if ( is_singular() ) :
    the_title( '<h1 class="entry-title">', '</h1>' );
else :
    the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
endif;

This is the solution I came up with:

$low_title_cleaned = esc_attr( $post->post_title );  // Is there a title?
$low_untitled = __( 'Untitled Document', 'laughter_on_water' ); // Internationalize...
if ( is_singular() && $low_title_cleaned ) { // Title h1
    the_title( '<h1 class="entry-title">', '</h1> title: ' );
} elseif ( !is_singular() && $low_title_cleaned ) { // Title h2
    the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
} elseif ( is_singular() && !$low_title_cleaned ) { // Untilled h1
    echo '<h1 class="entry-title">' . $low_untitled . '</h1>';
} elseif ( !is_singular() && !$low_title_cleaned ) { // Untilled h1
    echo '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $low_untitled . '</a></h2>';
}

Questions: Is this code reasonably secure?

Is there a shorter, more elegant way to manage the same?

Does it make sense to contribute something like this to core, wp-includes/post-template.php? It just returns nothing on line 46.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748930117a314911.html

最新回复(0)