Paulund
2013-11-12 #wordpress

When the_content() Ignores More Comment

In WordPress there is a special HTML comment called more. <!--more--> This comment allows you to put a stopping point in the content of the post, this is then searched for by WordPress when you display a list of posts. If it finds this HTML comment then WordPress will stop displaying the rest of the content and will add a continue reading → link. There are two ways of displaying the post content on a list of posts, you can either use the the_content() function or the the_excerpt() function. The **the_content() function ** will display the entire post including all images, when the the_excerpt() function will just display the first 55 words of the post. The more comment is only searched for on home page or front page, if the more comment is found it will only show the content before the the comment. The following code is an example of creating your own list of blog posts page, using the the_content() function to display the content of the post.


$wp_query = new WP_Query();
$wp_query->query('showposts=10' . '&amp;paged='.$paged);

<?php /* The loop */ ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <div class="entry-content">
        <?php the_content(); ?>
    </div>
<?php endwhile; ?>

But if this code isn't used on the front page or the home page the comment will be ignored and the list will always display the full content. ## Add More Global Variable

To use the more tag on lists not on the home page or front page you need to include the global variable $more.


global $more; 

// Search for the more tag
$more = 0;

// Ignore the more tag
$more = -1;

Using the $more variable you can define the value to use the more tag or ignore it, if the variable is zero then we make sure that WordPress searches for the more comment. If the value is -1 we are telling WordPress to ignore this comment and just display all the content. You can use this in your loop to make sure that WordPress searches for the more comment and only displays the content before the more comment.

$wp_query = new WP_Query();
$wp_query->query('showposts=10' . '&paged='.$paged);
<?php if ( $wp_query->have_posts() ) : ?>

<?php 
    global $more; 
    $more = 0;

    /* The loop */ 
?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
        <h1 class="entry-title">
            <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
        </h1>
    </header>

    <div class="entry-content">
        <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentythirteen' ) ); ?>
    </div>
    </article>
<?php endwhile; ?>

<?php else : ?>
    <?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>

Display Excerpt If Doesn't Exist

If the more comment doesn't exist then you can make sure that WordPress displays the excerpt by using the following code snippet, simply search for the more comment in the content, if it exists display the content else display the excerpt.


if( strpos( $post->post_content, '<!--more-->' ) ) {
    the_content( __( '&hellip; Read more <span class="meta-nav">&rarr;</span>' ) );
}
else {
    the_excerpt();
}