5

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()?

  • Not sure exactly where you're trying to run your code, but if you're on a single post view the $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!).. – t31os Jan 6 '12 at 19:28
1

Quite simply put: there is no built in function that does the same thing as your code above.

Bonus content: More tag tricks

7

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">&rarr;</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.

  • The idea of posting this answer here was to get all the codes in one place. :) – its_me Jul 22 '12 at 4:26
  • Don't count on 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 :) – Jeppe Mariager-Lam Dec 10 '17 at 4:12
1

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;
0

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.