web-dev-qa-db-fra.com

Afficher la description du CPT sur les pages archive.php pour tous les CPT

j'aimerais afficher la description du CPT sur la page archive.php, mais elle aimerait le faire de manière dynamique pour tous les CPT. Je veux dire sans spécifier le type actuel moi-même. Quelque chose comme the_archive_description mais pour CPT.

1
Michael Rogers

Depuis 4.9.

if ( get_the_post_type_description()) {

echo get_the_post_type_description();

}

redéclarer la fonction plantera WordPress.

2
Michael Rogers
function get_the_post_type_description() {
    $post_type = get_query_var( 'post_type' );

    if ( is_array( $post_type ) ) {
        $post_type = reset( $post_type );
    }

    $post_type_obj = get_post_type_object( $post_type );

    // Check if a description is set.
    if ( isset( $post_type_obj->description ) ) {
        $description = $post_type_obj->description;
    } else {
        $description = '';
    }
    return apply_filters( 'get_the_post_type_description', $description, $post_type_obj );
}

Voici une future fonction de WP v4.9 qui récupère la description d'une archive de type de publication et renvoie (chaîne) La description du type de publication. Je pense que cela peut faire l'affaire pour vous.

1
Kuliraj