get_extended()
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
String[]
. 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'] ); ?>
Changelog
Since 1.0.0 | Introduced. |