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:

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?

2 Answers
2

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.

Leave a Comment