Using the Post Type description in WordPress Themes

Sometimes, especially in the case of an items list, apart from the header/title of the list, we would want to add some text as an introduction for these items: a global suggestion, perhaps a couple of interesting links, a descriptive or in detail approach about the items, a definition, etc. When the items to list are inside a Category or a Tag, to add a header with description is obvious because, simply taking a look to the screen for editing terms, all of us know that they have a description but, do you know that post types have also got a description, and that you can use it in your themes?

An example of using the Post Type description in WordPress Themes

Yes, in the same way that with the terms of a taxonomy, WordPress Post Types have also got a field to add a description thus, you can use this property of the Post Type object to introduce any text that you want and, of course, afterwards you can use the value of this property inside your WP Themes.

For example, in the image, we can see an screenshot of an archive.php template listing Books. In this case, a text with the dictionary definition has been introduced in the property ‘description’ of the post type, but you could have introduced whatever other text, even with HTML tags inside, like in the example.

The method to introduce a description for any Post Types is just to add an element called description to the array used to declare the post type with register_post_type. See the example.

<?php
$args = array (
    // ... other important variables
    'description' => 'This is my description for this post type',
    // ... other important variables
);

register_post_type ( 'myposttype', $args );
?>

That’s all. You have already got a description for this post type. Of course, you can also use the translations module, or, as I said, even you can use HTML code in this description.

<?php
$args = array (
    // ... other important variables for post type definition
    'description' => __( '<h3>Book</h3><p>This is my <b>long</b> description for <span class="colored-pt">Books</span>.</p>', 'textdomain' ),
    // ... other important variables
);

register_post_type ( 'myposttype', $args );
?>

Then, inside any template, you can use this property. For example inside the archive.php you can show the description with this code.

<header>
// ... previous code
<?php
if ( is_post_type_archive() ) {
    global $wp_post_types;
    $pt = get_query_var ( 'post_type' );
    if ( is_string( $pt ) ) {
        if ( ! empty( $wp_post_types[ $pt ]->description ) ) {
            echo $wp_post_types[ $pt ]->description;
        }
    }
}
//...
?>
</header>

Try some combinations of text and styling, I’m sure you’ll reach for a perfect look.

By the way, if you have any doubt about how to register a new post type in WordPress, please, take a look to the official documentation of the function register_post_type for further information.

Have a nice WordPressing!

Leave a Comment

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>