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

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.

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

Related documentation: Content Templates

This topic contains 3 replies, has 2 voices, and was last updated by  Christian Cox 4 months ago. The support staff assigned to this topic is Christian Cox.

Viewing 4 posts - 1 through 4 (of 4 total)
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

Christian Cox
Forum moderator

Supporter languages: English (English )

Supporter Timezone: America/New_York (GMT-05:00)

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

Christian Cox
Forum moderator

Supporter languages: English (English )

Supporter Timezone: America/New_York (GMT-05:00)

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.

Viewing 4 posts - 1 through 4 (of 4 total)