Ignore More Tag in Post Content

[ad_1]

In WordPress, there is a special HTML comment tag called more.

<!--more-->

This tag allows you to put a stopping point in the content of the post, this is then searched by WordPress when you display a list of posts. If WordPress finds this more comment in your content then it will only display the content before the more tag and will add a “continue reading ?” link instead. The more tag is only searched for posts on homepage or frontpage. The problem with more tag arises on pages where we want to list posts except the frontpage or the homepage because WordPress ignores the more tag elsewhere and the list of posts will always display the full content.

To use the more tag on post stream not on the homepage as well as on frontpage, you need to include the global variable $more. Using the $more variable you can define if WordPress should honor the more tag or ignore it completely. If the variable is set to zero then we are asking WordPress to search for the more comment in content and use it as breaking post to display content before this comment. And if the value of $more variable is set to -1, then we are telling WordPress to ignore this comment and just display all the content.

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

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

Here is an example uses of the more comment and to only display the content before the comment.

<?php
// ignore more tag in post content
$args = array(
    'posts_per_page' => 10,
    'ignore_sticky_posts' => 1,
    'paged' => $paged
);
$wp_query = new WP_Query( $args );

if ( $wp_query->have_posts() ) :

    global $more; 
    $more = -1;

    while ( $wp_query->have_posts() ) : $wp_query->the_post();
        get_template_part( 'content', get_post_format() );
    endwhile;
else :
    get_template_part( 'content', 'none' );
endif;

wp_reset_postdata();
?>
[ad_2]

Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads