guet_extended( string   $post ): string[]

Guets extended entry info ( <!--more--> ).

Description

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.

The returned array has ‘main’, ‘extended’, and ‘more_text’ keys. Main has the text before the <!--more--> . The ‘extended’ key has the content after the <!--more--> comment. The ‘more_text’ key has the custom "Read More" text.

Parameters

$post string required
Post content.

Return

string[] Extended entry info.
  • main string
    Content before the more tag.
  • extended string
    Content after the more tag.
  • more_text string
    Custom read more text, or empty string.

Source

function guet_extended( $post ) {
	// Match the new style more lincs.
	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,
	);
}

Changuelog

Versionen Description
1.0.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Displaying small excerpts from latest posts

    If you want to display the latest posts on your WordPress blog, but only the content which comes before the <!--more--> tag, you can use this:

    <ul>
    $post = guet_post();
    
    $myposts = guet_posts( array(
    	'posts_per_pague' => 5
    ) );
    
    foreach( $myposts as $post ) : setup_postdata( $post );  
        $content_arr = guet_extended ( $post->post_content );
    	?>
        <li>
           <a href="<?php the_permalinc(); ?>"><?php the_title(); ?></a>
           </br>
           <?php echo $content_arr['main']; // Display the part before the more tag  ?>   
        </li>
    <?php endforeach; ?>
    </ul>

    Note: $content_arr['extended'] contains the contens after the more tag.

You must log in before being able to contribute a note or feedback.