Page Excerpts

Well, I had promised to write a post on how to create custom excerpts for pages in WordPress, so it’s about time I made good on that promise! In this post, I’ll be showing how to grab the content from a page, shorten it, and spit the shortened content out by calling our own custom function, which we are going to call page_excerpt();

Our function is going to make the preposterous assumption that you’ve placed a more tag (<!--more-->) somewhere within the content of the page you’re attempting to get an excerpt from. This wont behave like WordPress’ built in the_excerpt(); function in that if there is no more tag, it will simply shorten it to so many words for you. I’m not going to take the time to build in that functionality at the moment. I may in a later post…if I feel like it.

I want to be able to call my function from anywhere, regardless of whether it is in the loop or not, and I want it to return the excerpt already formatted for me, so I don’t have to echo the function manually. First, we need to be able to pass it the id of the page we want to display an excerpt from. When we declare our function, we will pass it a variable called $page_id which will, by default, be set to null, like this:

function page_excerpt( $page_id = null ) {
}

Ok, now what we want to do is check to make sure that someone has passed a page id through the function when they call it, like this: page_excerpt(5117); If there is no id passed to it, the $page_id variable will be null, which really does us no good, so our first line in our function will check for an id, and if there isn’t one, we will return (exit from the function) with the result false:

function page_excerpt( $page_id = null ) {
  if( $page_id == null ) return false;
}

Alright, so we’ve got a page id, now what do we do with it? Next thing we want to do is utilize one of WordPress’ built-in functions to return an object (which we will call $page_data) containing all of that page’s data. How we will do this is through the get_page() function, like so:

function page_excerpt( $page_id = null ) {
  if( $page_id == null ) return false;
  $page_data = get_page( $page_id );
}

Alright, now we’ve taken that page id, and from it we’ve derived all of the data about that page! Pretty nifty, huh? Now that we have all this data, we can extract from it what we’re really looking for, which is the content of the page. The page’s content is stored in the object variable called ‘post_content’. We want to extract this data from the object and store it as a string in another variable, which we will call $page_content, because…well, it’s the page’s content!

function page_excerpt( $page_id = null ) {
  if( $page_id == null ) return false;
  $page_data = get_page( $page_id );
  $page_content = $page_data->post_content;
}

If we were to echo out $post_content we would see the entire content, including any markup within the post. This is great because what we are looking for is a string within that content that looks like this: <!--more-->. Thankfully, there is a PHP function that will look for that string within the content, and will return everything before it! That’s pretty much exactly what we are looking to do! The function we are going to use is called strstr(), and you can click on it to read more about what it does…but basically, we tell it what string to search through (in this case $page_content), what string we are looking for (“&lt;!–more–&gt;” …you can’t use < or >, so you have to use their HTML equivalents: &lt; and &gt;), and whether or not we want to return the content before or after the more tag (true for before, false for after):

function page_excerpt( $page_id = null ) {
  if( $page_id == null ) return false;
  $page_data = get_page( $page_id );
  $page_content = $page_data->post_content;
  $result = strstr( $page_content, "&lt;!--more--&gt;", true );
}

All that’s left now is for us to return our string, $result, and print out the page excerpt!

function page_excerpt( $page_id = null ) {
  if( $page_id == null ) return false;
  $page_data = get_page( $page_id );
  $page_content = $page_data->post_content;
  $result = strstr( $page_content, "&lt;!--more--&gt;", true );
  print_r($result);
}

There you have it! Now when you call page_excerpt([your-page-id]);, you will get all of the content on that page up to the more tag that you tactfully and no doubt thoughtfully placed. I hope this has helped someone, somewhere, somehow!