Using the More tag with a custom loop in WordPress

I was using custom posts to produce a list of projects and everything was going fine. I had a loop using WP_Query that filtered the posts correctly on post type.

However when I tried using the <!–more–> tag in the custom posts nothing happened. The complete post still appeared and there was no break at the specified point.

The solution was in an article on the WordPress forums specifying that the following code should be inserted before the_content was invoked:

global $more; 
$more = 0;

The trick is to insert the code above inside the loop, it will not work if you add it outside the loop, it has to be immediately before the_content for this to work, as in the example below:

$loop = new WP_Query( array( 'post_type' => 'project', 
                             'posts_per_page' => 5 ));

while ( $loop->have_posts() ) : $loop->the_post();
		  global $more; 
                  $more = 0;	/** cure more tag problems */	
		  echo '<div class="entry-content" id="projects">';
		  the_content('read more...');
		  echo '</div>';

After making these changes the more tag worked correctly. Whether this is the correct way to do this is another question, for example some may question the need for a global variable, but it does work and solved the problem.

This entry was posted in WordPress. Bookmark the permalink.

2 Responses to Using the More tag with a custom loop in WordPress

We would love to hear from you so please leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.