theme development - Is there a has_more_tag() method or equivalent?

admin2025-06-04  4

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

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

Share Improve this question asked Jan 5, 2012 at 16:44 N2MysticN2Mystic 3,1937 gold badges47 silver badges72 bronze badges 1
  • 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 Commented Jan 6, 2012 at 19:28
Add a comment  | 

4 Answers 4

Reset to default 2

Quite 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.

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.

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/reference/functions/get_extended/

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

最新回复(0)