Head Meta Pro provides a wealth of shortcut variables for adding dynamic bits of data like %%DATE_YEAR%%
, %%SITE_NAME%%
, and much more. This quick post explains how to customize the %%POST_DESC%%
shortcut, which outputs a description of the current post’s content.
A common use for %%POST_DESC%%
is added to the “description” meta tag, like so:
<meta name="description" content="%%POST_DESC%%">
That will output the following meta tag in the <head>
section of the post:
<meta name="description" content="Head Meta Pro provides a wealth of shortcut variables for adding dynamic bits of data like %%DATE_YEAR%%, %%SITE_NAME%%, and much more. This quick post …">
This shows how the %%POST_DESC%%
shortcut outputs the first 155 characters of post content (read popout note below for details). This number currently is ideal for search engine results, but you may (for whatever reason) prefer to output a different number of characters. If so, the next section explains how to do it with a quick code snippet.
Change the number of characters
By default, %%POST_DESC%%
outputs the first 155 characters of the post content. To change the default 155-character limit, add the following code snippet to your site:
function head_meta_pro_post_desc_length($chars) {
return 155; // change to any number
}
add_filter('head_meta_pro_post_desc_length', 'head_meta_pro_post_desc_length');
Simply change the 155
to whatever number of characters you would like output for the %%POST_DESC%%
shortcut variable. Note: while it is possible to change the length of %%POST_DESC%%
, search engines such as Google have limits on the number of characters displayed in search results. Also just fyi, here is a guide that explains how to add custom code to your site.
Change the ellipsis character
Another way that %%POST_DESC%%
may be customized is by changing the character that is appended to its output. For example, notice the ellipsis …
used to truncate the meta description for this post:
<meta name="description" content="Head Meta Pro provides a wealth of shortcut variables for adding dynamic bits of data like %%DATE_YEAR%%, %%SITE_NAME%%, and much more. This quick post …">
To change that to any character(s), add the following code snippet to your site:
function head_meta_pro_post_desc_append($char) {
return ' …'; // change to any character
}
add_filter('head_meta_pro_post_desc_append', 'head_meta_pro_post_desc_append');
You can change the …
to any character that makes sense. Like maybe just a period. Or a dollar sign. Or anything you want. Note that by default, the plugin adds a blank space before the ellipsis, just fyi.
…
, it actually means this: &
hellip;
. Take-home lesson: when defining your meta tags always use HTML entities for any special characters.Difference between %%POST_DESC%% and %%POST_EXCERPT%%
For anyone familiar with the plugin’s shortcut variables that may be wondering. How is %%POST_DESC%%
different than %%POST_EXCERPT%%
? The difference is subtle. The %%POST_DESC%%
shortcut gets its text from the post content, whereas %%POST_EXCERPT%%
gets its text from the post excerpt, which is generated by WordPress. So basically you get a bit more control when using %%POST_DESC%%
. For example you can set the number of characters and appended character, as explained in the post above.