Forums

[resolved] Conditionally check for `<!--more-->` quick tag (5 posts)

  1. david.brunelle
    Member
    Posted 2 years ago #

    I've been trying to figure out how I can check for the existence of the "more" quick tag in a post. This is the scenario:

    • If the author has used the "more" quick tag, then treat the post normally.
    • if the author didn't use the "more" tag, then only show an excerpt

    In code terms:

    <?php
    if($more_link) {
    	the_content();
    }
    else {
    	the_excerpt();
    }
    ?>

    Any ideas?

  2. alchymyth
    The Sweeper
    Posted 2 years ago #

    <?php
    if(strpos(get_the_content(),'more-link') === false) {
    	the_excerpt();
    }
    else {
    	the_content();
    }
    ?>
  3. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    How about:

    <?php
    if( strpos( $post->post_content, '<!--more-->' ) ) {
    	the_content();
    }
    else {
    	the_excerpt();
    }
    ?>
  4. david.brunelle
    Member
    Posted 2 years ago #

    Thanks alchymyth & esmi. Here's what I came up with (around the same time you both replied):

    <?php
    if(preg_match('/<!--more(.*?)?-->/', $post->post_content)) {
    	the_content();
    }
    else {
    	the_excerpt();
    }
    ?>
  5. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    I think you'll find that strpos() is more efficient/faster and has less CPU impact than preg_match().

Topic Closed

This topic has been closed to new replies.

About this Topic