I need to determine if the current post has a "more" tag. I'm currently using
$pos=strpos($post->post_content, '<!--more-->');
Am I missing a built in method similar to has_excerpt()?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityQuite simply put: there is no built in function that does the same thing as your code above.
Bonus content: More tag tricks
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.
<?php
if( strpos( $post->post_content, '<!--more-->' ) ) {
the_content();
}
else {
the_excerpt();
}
?>
(Credit: MichaelH)
<?php
if( strpos( get_the_content(), 'more-link' ) === false ) {
the_excerpt();
}
else {
the_content();
}
?>
(Credit: Michael) Basically does #1 the other way around.
<?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.
get_the_content()
to return more-link
in all cases (I got a span with an id of more-214
for example). Use $post->post_content
and look for <!--more-->
instead :)
Dec 10, 2017 at 4:12
I could not get any of the provided solution to work, however I found this to be working well for me, publishing it as an extra solution if anybody else had problems getting it working. Just testing if the content is them same with and without stripping the teaser.
// Choose the manual excerpt if exists
if ( has_excerpt() ) :
the_excerpt();
// Is there a more tag? Then use the teaser. ()
elseif ( get_the_content('', false) != get_the_content('', true) ) :
global $more;
$more = 0;
echo strip_tags(get_the_content( '', false ));
$more = 1;
// Otherwise make an automatic excerpt
else :
the_excerpt(40);
endif;
For the ones searching for a more WP related answer, You can use this logic:
$info = get_extended($post->post_content);
if(!empty($info["extended"])){
// it has a read more tag.
}else{
// it hasn't one.
}
For this, you can blame the WP Core if it doesn't work right. :)
Reference: https://developer.wordpress.org/reference/functions/get_extended/
$pages
global should hold a value representing the number of pages(if the post is paged, ie. has content that spans pages, eg. it has more!)..