How to Develop a Custom Page Excerpt Plugin for WordPress?
It’s easy to show lists of posts in WordPress. Your theme’s index.php file is probably handling all your categories, authors and search requests. However, sometimes you need a little more control. Perhaps you have multiple product pages but only want to feature two or three on your home page without manually re-entering the details.
In this tutorial, we’ll develop a small WordPress plugin which replaces a [shortcode] with a link to a specific page or post. The link will show the title, thumbnail and excerpt, but you can configure it to show which ever details you require.
Create a new file named showexcerptlink.php in your plugins folder (wp-content/plugins) and add a header so it can be identified by WordPress:
<?php
/*
Plugin Name: Show Page/Post Excerpt
Plugin URI: http://www.prashantblog.com/
Description: Replaces a shortcode with a link to a specific page or post
Version: 1.0
Author: Prashant Kumar
Author URI: http://prashantblog.net/
License: Use this how you like!
*/
This is followed by our primary function, ShowExcerptLink(). We require one parameter: the slug/permalink name. While we could pass the ID, the slug is a better option because it’s less likely to change while you’re editing pages. You could add further parameters to control the HTML output.
function ShowExcerptLink($params = array()) {
extract(shortcode_atts(array(
'slug' => ''
), $params));
$html = '';
if ($slug == '') return $html;
We can now lookup the page or post using the WordPress WP_Query object. The following code attempts to find the page slug but, if that doesn’t exist, it looks for a post slug instead:
$q = new WP_Query("pagename=$slug");
if (!$q->have_posts()) {
$q = new WP_Query("name=$slug");
}
We can now start a WordPress loop — although it’ll only have zero or one post:
// the loop
while ($q->have_posts()) {
$q->the_post();
The HTML output is now generated in the $html string. You can use any standard function that you’d normally find in a WordPress loop. In this example, we’ll output the title (H2), thumbnail, and excerpt:
// generate HTML
$link = '<a href="' . get_permalink() . '>';
$html .=
'<h2>' . $link . the_title('','',false) . "</a></h2>\n" .
(has_post_thumbnail() ?
$link . get_the_post_thumbnail() . '</a>' : ''
) .
get_the_excerpt();
(This code is valid in any version of HTML although HTML5 would permit the anchor around the
whole block.)
We can now end the loop, return the HTML string and complete the function:
}
return $html;
}
Finally, we’ll register our function as a shortcode handler:
// register shortcode
add_shortcode('showexcerptlink', 'ShowExcerptLink');
Save the file and activate the plugin in your WordPress control panel.
Showing an Excerpt
The following shortcode can now be added to the content of any page or post:
[showexcerptlink slug=page-or-post-slug-name]
If you’re referencing a page which has one or more parent pages, the slugs must be separated by a forward slash, e.g.
[showexcerptlink slug=grand-parent-slug/parent-slug/page-slug]