get_extended()

You are here:

get_extended( string $post )

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

Description 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.


Top ↑

Parameters Parameters

$post

(string) (Required) Post content.


Top ↑

Return 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.

 


Top ↑

Source Source

File: wp-includes/post.php

721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
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,
    );
}


Top ↑

Changelog Changelog

Changelog
VersionDescription
1.0.0Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content

    You must log in to vote on the helpfulness of this noteVote results for this note:0You must log in to vote on the helpfulness of this note
    Contributed by Codex — 

    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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <ul>
    $post = get_post();
     
    $myposts = get_posts( array(
        'posts_per_page' => 5
    ) );
     
    foreach( $myposts as $post ) : setup_postdata( $post ); 
        $content_arr = get_extended ( $post->post_content );
        ?>
        <li>
           <a href="<?php the_permalink(); ?>"><?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 contents after the more tag.

 

Was this article helpful?
Dislike 0
Views: 2

Cart

Log in

Create an Account
Back to Top