1

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?

4
  • What have you tried? Please read How to Ask for more info on how to rework your question. – kaiser Oct 21 '13 at 13:00
  • @kaiser , any post/page can have text, right? i place more tag after a few sentences. i just want to be able to fetch the text before more tag. it is also possible to get page by path, right? so, say i need to display a certain page's excerpt on the front page or any other page. let me know if the info's not enough. thanks! – Alex Herrmann Oct 21 '13 at 13:53
  • Please always put additional info as edit into your question. – kaiser Oct 21 '13 at 14:21
  • @kaiser , have already put the info. hope this is enough. thanks! – Alex Herrmann Oct 22 '13 at 10:21
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.

1
  • Good morning and thank you for your feedback! I've extended the question with the code lines (see above) I use to fetch the content of the current front page before more tag. Do you have any idea what the filter needs to be to allow me access any page? Thank you a lot mate! – Alex Herrmann Oct 22 '13 at 10:02
7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.