How to get the excerpt of a page before more tag?

I have many pages in my wp installation. All I want is to be able to display the excerpt of any page, also on any page (before more tag). How do I go about it?

I was fetching the content of the current (front) page through this code:

Read More
global $more;
$temp = $more;
$more = false;
echo get_the_content( '' );
$more = $temp;

(I use these lines in my front page template)

But I have no idea of how do I fetch the content of any page. Maybe I need to do manipulations with add~/remove_filter changing the page id/slug/title/etc?

Related posts

2 comments

  1. The content is manipulated by the get_the_content function, and that function always assumes the current post in the Loop. You can’t pass it an ID, strangely.

    The easiest way to get just the bit before the more for an arbitrary post ID is:

    $p = get_post(1);
    $p = preg_split( '/<!--more(.*?)?-->/', $p->post_content );
    echo $p[0];
    

    There may be ways to hijack the global $post variable but unless you really need the complex manipulation of get_the_content I wouldn’t bother. You can always create a near clone of that function that will allow you to pass an ID into it.

  2. Another way to get the excerpt, by that I mean the text before the <!--more--> tag, is to use the following function: get_extended()

    //somewhere in the loop...
    global $post;
    $content = get_extended( $post->post_content );
    $excerpt = $content['main'];
    $main_content = apply_filters('the_content', $content['extended']);
    

    As it says in the WordPress codex:

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

Comments are closed.