WordPress at a glance

get_extended() WP 1.0.0

Divides content into 2 parts: before <!--more--> and after it. Returns an array with the resulting parts.

There should not be any space after the second dash and before the word 'more'. There can be text or space(s) after the word 'more', but won't be referenced.

No Hooks.

Return

Array. Post before ('main'), after ('extended'), and custom read more ('more_text').

Array(
	[main]      => 'some text', // text before the `<!--more-->` comment.
	[extended]  => 'some text', // text after the `<!--more-->` comment.
	[more_text] => ''           // if more is specified as: <!‐‐more reed more…‐‐>, 
								// then this value will contain "reed more…"
)

Usage

$parts = get_extended( $post_content );
$post_content(string) (required)
The content of the post, which contains <!--more--> comment and which you want to split.

Examples

#1 Display first part of last 5 posts

If you want to display the latest posts on the blog, but you need to display only part of the text before the <!--more--> tag, use this code:

<ul>
	<?php
	global $post;

	$myposts = get_posts( array( 'numberposts'=>5 ) );

	foreach( $myposts as $post ){
		setup_postdata( $post );  
		$parts = get_extended( $post->post_content ); ?>
		<li>
		   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
		   </br>
		   <?php echo apply_filters( 'the_content', $parts['main'] ); // part before more ?>   
		</li>
	<?php
	}
	wp_reset_postdata();
	?>
</ul>

#2 Display advertising immediately after the <!--more--> tag

Let's say we need to display an ad block after the <!--more-->, on a separate page (file single.php). To do this, replace the_content() with the following code:

<?php 
global $post;
$content_parts = get_extended( $post->post_content );

echo apply_filters( 'the_content', $content_parts['main'] );

echo "advertising  code here";

echo apply_filters( 'the_content', $content_parts['extended'] );
?>

Code of get_extended: wp-includes/post.php VER 5.0

<?php
function get_extended( $post ) {
	//Match the new style more links.
	if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
		list($main, $extended) = explode($matches[0], $post, 2);
		$more_text = $matches[1];
	} else {
		$main = $post;
		$extended = '';
		$more_text = '';
	}

	//  leading and trailing whitespace.
	$main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
	$extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
	$more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text);

	return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text );
}

Related Functions

From tag: excerpt (fragment, snippet)

More from category: Other

No comments
    Hello, !     Log In . Register