Display Category Descriptions with a Shortcode in WordPress

Do you use WordPress and want an easy way to include category descriptions in your post? I did, so I created a shortcode for just that purpose. Here’s how to display category descriptions in your WordPress posts or pages with a custom shortcode.

The need to display category descriptions

On RechargeWorkshop.com, I post recurring themes and sometimes series of posts where the need for repeated content arises. Normally, you don’t want to repeat content for SEO reasons, but a simple intro paragraph to get the reader up to speed makes sense and can be very useful.

However, after copy-pasting multiple times in multiple posts, I found it tedious and thought there must be a better way. Since my intro paragraph was more or less just the category description for that category of posts, I needed a way to drop in the description on each post as easily as possible.

It’s worth mentioning that this solution has the added benefit of allowing you to update the category description in one location and have it be reflected everywhere you’re referencing it. That is a really powerful benefit.

The shortcode… code

add_shortcode('cat_desc', 'cat_desc_shortcode');
function cat_desc_shortcode( $atts, $content = null ) {
	$a = shortcode_atts( array(
		'id' => null,
		'slug' => null,
		'title' => 'false',
		'link' => 'true',
	), $atts );

	if( $a['id'] ) {
		$cat = get_category( $a['id'] );
		$a['title'] = 'true';
	} elseif( $a['slug'] ) {
		$cat = get_category_by_slug( $a['slug'] );
		$a['title'] = 'true';
	} else {
		$cat = get_the_category()[0];
	}

	if( $cat == null ) return;

	$cat->url = get_category_link( $cat->term_id );

	$output = ( $a['title'] == 'true' ) ?
				'<blockquote class="cat_desc has_title"><h3><a href="'.$cat->url.'"><span>Category:</span> '.$cat->name.'</a></h3>' :
				'<blockquote class="cat_desc">';
	$output .= '<div class="description">'.$cat->description.'</div>';
	$output .= ( $a['link'] == 'true' ) ? '<a class="link" href="'.$cat->url.'">View all in '.$cat->name.' &raquo</a>' : '';
	$output .= '</blockquote>';

	return $output;
}

This code can live in your functions.php file in your theme or you can create a shortcode file that you reference there. Alternatively, you may choose to use a plugin that lets you configure shortcodes through the admin. You will have to modify this code to fit that environment, but it shouldn’t require much alteration.

So what’s it doing?

It should be fairly easy to read through, so I’ll just touch on the basics. I’ve called the shortcode cat_desc to keep it short, but feel free to name it whatever you like.

The attributes are as follows:

id specifies the category ID so you can reference a specific category. This is useful when creating a listing of the different categories on your blog and ensures the correct category will be displayed.

slug can be used instead of id, but you’ll have to update it if you change your category slug in the future.

title toggles the display of the category title before the description.

link toggles the display of the category link after the description.

See the examples below for how they are used. After the attributes are defined, the function sets up the category object and returns empty if no category is found. Then the layout and formatting is done within the $output variable.

I’ve included a few styling features in my output and used a blockquote as my primary element. If you’d like to change these, feel free. It’s all up to you. Just retain the necessary variable output and the rest is formatting.

Examples

At its most basic usage, the shortcode [cat_desc] will display the current category description. This doesn’t work well for posts with multiple categories because it may not display the category you’re expecting. In that case, you may specify the id of the category.

Default / no attributes

The default output of the shortcode, with no attributes, is the current post’s category.
Example: [cat_desc].

In this category, you'll find all my posts on web design, web development, and SEO. I used to work a lot with Anchor CMS, so you'll find some older tutorials about that. Now I mostly work with WordPress and I have a number of tutorials, as well as tips and tricks that I share here. Search Engine Optimization (or SEO) and Google Analytics are two more popular topics you'll find articles on as well.
View all in Web Design »

Specifying the category

Category ID
Control which category is shown with the id attribute.
Example: [cat_desc id=7].

Category: Woodworking

Do you like woodworking tools and projects? Me too.
View all in Woodworking »

Category slug:
Control which category is shown with the slug attribute. Note that If id is set, it will take precedent, and the slug will be ignored.
Example: [cat_desc slug=diy]

Category: DIY

These are posts all about Do-It-Yourself projects and practices. Most of my projects and DIY tips get published to RechargeWorkshop.com. But anything else might just make it into this category.
View all in DIY »

Add/remove the title

By default, the category name is only shown when the id or slug has been specified. Modify this behavior with theĀ title attribute.
Example: [cat_desc title=true].

Category: Web Design

In this category, you'll find all my posts on web design, web development, and SEO. I used to work a lot with Anchor CMS, so you'll find some older tutorials about that. Now I mostly work with WordPress and I have a number of tutorials, as well as tips and tricks that I share here. Search Engine Optimization (or SEO) and Google Analytics are two more popular topics you'll find articles on as well.
View all in Web Design »

Removing the lower link

By default, a link to the category is shown after the description. Modify this behavior with the link attribute.
Example: [cat_desc link=false].

In this category, you'll find all my posts on web design, web development, and SEO. I used to work a lot with Anchor CMS, so you'll find some older tutorials about that. Now I mostly work with WordPress and I have a number of tutorials, as well as tips and tricks that I share here. Search Engine Optimization (or SEO) and Google Analytics are two more popular topics you'll find articles on as well.