Add the Excerpt to Recent Posts
The Recent Posts element in Cornerstone are admittedly lean, but one simple change you can make is to add the post excerpt to them.
While this is something that gets asked frequently, and the code needed to achieve it has been posted by Themeco, many can benefit by having it here. We’re much easier to find than a forum search!
Here’s how you can add excerpts into recent posts.
The following code should be placed in your functions.php file inside your x-child theme.
Furthermore, it’s good to understand how something functions. The code above works by removing the recent posts shortcode from WordPress, and re-initializing it with a new function that pulls the post excerpt from the database and places it into the post box. Finally, you can use CSS to customize the look and feel of the text simply by targeting the .x-recent-posts-excerpt class.
When I tried this, I received a “fatal error”:
Fatal error: Call to undefined function csl18n() in /nfs/c03/h06/mnt/56870/domains/MYWEBSITE/html/wp-content/themes/x-child/functions.php on line 93
This is the only thing I have added to the child theme.
Hi Amy, sorry about that! We are going through and updating old tips to comply with the new X releases. I’ve fixed the code above, it should work for you now. Cheers!
Wow–thank you for responding so quickly! I can only imagine how many of these fixes you will need to find!
That said, I’m still getting the same error using your new code. 🙁
Actually that’s alie. Now the error is on line 91:
Fatal error: Call to undefined function csl18n() in /nfs/c03/h06/mnt/56870/domains/MYWEBSITE/html/wp-content/themes/x-child/functions.php on line 91
Strange, it’s not in the code (maybe cache got the best of us!). Replace every instance of csl18n() with ‘__x__’ (including the single quotes)
It works–you are a genius!!!! Thank you for providing these code tips!
I was wondering if there was a way to pull in the categories too? I tried just modifying what you have here, but it just pulls text that says “ARRAY”. I haven’t been able to find anything on the forums about adding categories to the recent posts widget.
Hey Kevin, anytime you see array while echoing, it means it’s an actual array and not a string. You will have to loop through it. do a print_r() on the variable to see the structure and the data first to make it easier. Good luck!
Is there a way to choose the length of the excerpt?
Not off the top of my head. X hooks in to the excerpt filter and sets it’s own length (55 words). You would have to cancel that and set your own.
I have a custom post type of ‘recipes’ that I want to use this for, is there a way to include this PT in the available options for the recent posts.
Hi Stuart, the shortcode for this function accepts the attribute for type, so you could do [x_recent_posts type="recipes"] in order to show those posts. Editing the function to include multiple post types is a little out of scope for comment help, however 🙂
Had to update to WordPress 5.5 to work without error, but once I did worked like a charm.