Steve Taylor photo

Standalone “more tag” processing for WordPress

A WordPress site I’m working on needs automatic listing of sub-pages on certain pages, including excerpts from the sub-pages’ content. I’m having to grab the sub-pages with a custom query due to complex custom field matching, so I’m not using the_content() for outputting. How to make use of the “more tag”, the handy placeholder that WP uses to extract teaser copy from the full contents?

The more tag isn’t applied to pages by WP, so a little bit of custom coding would be in order anyway. Here’s my basic function:

function slt_moreTag( $content, $return = false ) {
	if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
		$content = explode( $matches[0], $content, 2 );
		$teaser = $content[0];
	} else {
		$teaser = $content;
	}
	$teaser = apply_filters( 'the_content', $teaser );
	if ( $return ) {
		return $teaser;
	} else {
		echo $teaser;
	}
}

It’s based on the processing in get_the_content() in /wp-includes/post-template.php, which is the place to check if you want to include any extra processing, like handling the “Read More” linking (my function doesn’t). All this function does is:

  1. Takes some raw content
  2. Searches for , and strips it and subsequent text if found
  3. Applies the standard WP content filters
  4. Returns or outputs the result

Hopefully useful for someone doing some customization of their own…

One comment

  1. thanks a lot! I had exactly the same problem but i’m not WP-experienced enough to create that function on my own.. thank you for sharing

Comments are closed.