Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Best I can find:

    $obj = get_post_type_object( 'my_post_type' );
    echo $obj->description;

    Kind of bothers me that WP Core doesn’t have a function for this, but it is what it is.

    Thread Starter tactics

    (@tactics)

    Nope, doesn’t work. But thanks.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Just to be certain, you are passing in the actual post type slugs with the snippet above, and not just “my_post_type” right?

    Thread Starter tactics

    (@tactics)

    My goof, you’re right, that works. Thanks Michael.

    $obj = get_post_type_object( 'my_post_type' );
    echo $obj->description;

    Sorry for this noob question how could I add this in the functions.php of my child theme? On a custom post type archive I want to show the description of the custom post type below the archive title. The custom posts are showing up fine.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    2alex, you shouldn’t need to add it via functions.php unless you want to make a function specifically to handle such a thing. Otherwise you could just copy/paste exactly as is and it’d work fine. Just be sure to change “my_post_type” to the actual post type slug.

    If you really want:

    function 2alex_post_type_description( $post_type = '' ) {
        $obj = get_post_type_object( $post_type );
    
        if ( ! empty( $obj ) ) {
            echo $obj->description;
        }
    }

    Usage:

    <p><?php 2alex_post_type_description( 'my_post_type' ); ?></p>

    Thanks Michale awesome fast answer. Stupid noob question copy/paste where?
    Maybe my scenario helps to better understand what I am after see this topic Showing CPT description in archive

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If you’re going for the custom function method I typed up, then the function declaration goes in your functions.php file, and the usage example goes in whatever file you need to display it in. Sounds like archive.php would be the best bet. If you’re not going to do the custom function, then just use the “$obj = get_post_type_object( ‘my_post_type’ ); echo $obj->description;” bit in archive.php.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Display custom post type description in template?’ is closed to new replies.