Wordpress - Is there a has_more_tag() method or equivalent?
Making a quick note of the codes we could use to show the_content();
if the More tag exists, and the_excerpt();
if it doesn't.
Code #1 (Recommended)
<?php
if( strpos( $post->post_content, '<!--more-->' ) ) {
the_content();
}
else {
the_excerpt();
}
?>
(Credit: MichaelH)
Code #2
<?php
if( strpos( get_the_content(), 'more-link' ) === false ) {
the_excerpt();
}
else {
the_content();
}
?>
(Credit: Michael) Basically does #1 the other way around.
Code #3
<?php
if( preg_match( '/<!--more(.*?)?-->/', $post->post_content ) ) {
the_content();
}
else {
the_excerpt();
}
?>
(Credit: helgatheviking) For use only in edge cases where you cannot use strpos()
. Generally strpos()
is more efficient than preg_match()
.
Making it more conditional:
<?php
if ( is_home() || is_archive() || is_search() ) {
if( strpos( $post->post_content, '<!--more-->' ) ) {
the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) );
}
else {
the_excerpt();
}
}
else {
the_content();
}
?>
What does it do? If the page shown is home, archive or search results page, then show the_content();
if the More tag exists, the_excerpt();
if it doesn't, and simply show the_excerpt();
on all other pages.
Quite simply put: there is no built in function that does the same thing as your code above.
Bonus content: More tag tricks