Skip Navigation

[Resolved] How do I show the post type description in a post type archive?

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display the description of a custom post type in the archive of that custom post type. The shortcode I found for non-archive pages does not work on archives.

Solution: Add the following code to functions.php to enable a shortcode on archive pages:

function get_post_desc_for_archive( $atts,$content ) {
    global $wp_post_types;
    global $WP_Views; 
    $post_type = $WP_Views->post_query->query['post_type'];
    $obj = $wp_post_types[$post_type];
    return $obj->description;
}
add_shortcode( 'get_cpt_desc_for_archive', 'get_post_desc_for_archive' );

Use the shortcode on archive pages like this:

[get_cpt_desc_for_archive]

Relevant Documentation: https://codex.wordpress.org/Shortcode_API
https://codex.wordpress.org/Function_Reference/get_post_type_object

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#559212

Tell us what you are trying to do?
I am trying to show the post type description in a post type archive template. I would like it to be at the top of the page, placed via a custom shortcode, above the loop.
Is there any documentation that you are following?
I've searched your forums and can only find instructions on how to add code to functions.php to show post type descriptions in content templates, not on archive pages. Taxonomy archive descriptions are available as a native shortcode already, but not post type descriptions.
Is there a similar example that we can see?

What is the link to your site?

#559356

Hi, you can use the same code, modified slightly for post type archives:

function get_post_desc_for_archive( $atts,$content ) {
    global $wp_post_types;
    global $WP_Views;

    $post_type = $WP_Views->post_query->query['post_type'];
    $obj = $wp_post_types[$post_type];
    return $obj->description;
}
add_shortcode( 'get_cpt_desc_for_archive', 'get_post_desc_for_archive' );

Then the shortcode will be:

[get_cpt_desc_for_archive]
#559531

Works perfectly. Thanks!

#559536

Hi, I just noticed that I left in an error_log statement by mistake. I have removed that statement from the code above. This statement can be removed from your code as well. It won't affect your code if you leave it in place, but it may write a message into your server logs.