最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Underscores Theme Unit Testing - Catching Untitled Posts

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far