Using WordPress ‘get_the_post_type_description()’ PHP function

The get_the_post_type_description() WordPress PHP function retrieves the description for a post type archive.

Usage

echo get_the_post_type_description();

Parameters

  • None

More information

See WordPress Developer Resources: get_the_post_type_description()

Examples

Display Post Type Description

Display the description of the post type archive on an archive template.

if (is_post_type_archive()) {
    // Retrieve the post type description
    $post_type_description = get_the_post_type_description();

    // Display the description
    echo '<p>' . $post_type_description . '</p>';
}

Add Post Type Description to Archive Title

Add the post type description to the archive title using the get_the_archive_title filter.

function add_description_to_archive_title($title) {
    if (is_post_type_archive()) {
        $post_type_description = get_the_post_type_description();
        $title .= ': ' . $post_type_description;
    }
    return $title;
}
add_filter('get_the_archive_title', 'add_description_to_archive_title');

Display Post Type Description in Widget

Create a custom widget that displays the post type description on an archive page.

class Post_Type_Description_Widget extends WP_Widget {
    // Initialize the widget
    function __construct() {
        parent::__construct(
            'post_type_description_widget',
            'Post Type Description',
            array('description' => 'Displays the post type description on archive pages')
        );
    }

    // Display widget output
    function widget($args, $instance) {
        if (is_post_type_archive()) {
            $post_type_description = get_the_post_type_description();
            echo $args['before_widget'];
            echo $args['before_title'] . 'Description' . $args['after_title'];
            echo '<p>' . $post_type_description . '</p>';
            echo $args['after_widget'];
        }
    }
}
add_action('widgets_init', function() {
    register_widget('Post_Type_Description_Widget');
});

Display Post Type Description in Navigation Menu

Add the post type description as a submenu item for post type archive menu items using the wp_nav_menu_objects filter.

function add_post_type_description_to_menu($items) {
    $new_items = array();

    foreach ($items as $item) {
        $new_items[] = $item;
        if ($item->object === 'post_type_archive') {
            $post_type_description = get_the_post_type_description();
            $description_item = new stdClass();
            $description_item->title = $post_type_description;
            $description_item->menu_item_parent = $item->ID;
            $description_item->ID = 'post_type_description_' . $item->ID;
            $description_item->db_id = 0;
            $description_item->url = '';
            $description_item->classes = array('post-type-description');
            $new_items[] = $description_item;
        }
    }
    return $new_items;
}
add_filter('wp_nav_menu_objects', 'add_post_type_description_to_menu');

Conditionally Display Post Type Description

Display the post type description only if it’s longer than a certain length.

$post_type_description = get_the_post_type_description();
if (strlen($post_type_description) > 50) {
    echo '<p>' . $post_type_description . '</p>';
}