Affichez tous les articles d'un type de post personnalisé

Affichez tous les articles d’un type de post personnalisĂ©

Pensez à créer un THEME-CHILD.

Dans votre fichier .php, ajouter le code suivant.

Notes :
• Adaptez le code. Modifiez « post » par le Custom Post Type de votre choix.
• Si le code ne fonctionne pas, c’est certainement que le Custom Post Type ne dispose pas d’une des fonctionnalitĂ© utilisĂ©e (ex : la taxonomie « category » n’est pas ajoutĂ©e au C.P.T.).

<h2>Articles de la catégorie</h2>

<?php // USEFUL FUNCTIONS
// Custom length for excerpt
function limitCharStr($str, $length){
    $new_str = substr($str,0,$length);
    if( strlen($str) !== strlen($new_str) ) $new_str .= "...";
    return $new_str;
}
?>

<?php // GET POSTS WE WANT
$args = array(
    'post_type'       => 'post',
    'posts_per_page'  => -1,
    // You can add different conditions, like category :
    //'category_name'   => '',
    'orderby'         => 'title',
    'order'           => 'ASC',
);
$posts = get_posts($args);
?>

<?php // FOR EACH POST, DO...
foreach($posts as $post) :
    // Post variables
    $post_id       = $post->ID;
    $post_guid     = $post->guid;
    $post_title    = $post->post_title;
    $post_excerpt  = limitCharStr( $post->post_excerpt , 80 );
    // Category (category is a term)
    $post_category            = get_the_category($post_id)[0];
    $post_category_id         = $post_category->term_id;
    $post_category_name       = $post_category->name;
    $post_category_slug       = $post_category->slug;
    $post_category_thumbnail  = get_term_meta($post_category_id,'img',true)[guid];
    $post_category_all        = get_the_category_list(" > ", "multiple", $post_id);
    // Get post thumbnail. If empty get category thumbnail
    $post_thumbnail = get_the_post_thumbnail_url($post_id);
    if ( strlen($post_thumbnail) < 1 ) $post_thumbnail = $post_category_thumbnail;
?>

<a href="<?php echo $post_guid ?>">
    <div class="post">
        <div class="img-container">
            <img src="<?php echo $post_thumbnail ?>">
        </div>
        <div class="text-container">
            <div class="post-line">
                <span class="post-category"><?php echo $post_category_name ?></span>
            </div>
            <div class="post-line">
                <span class="post-title"><?php echo $post_title ?></span>
            </div>
            <div class="post-line">
                <span class="post-excerpt"><?php echo $post_excerpt ?></span>
            </div>
        </div>
    </div>
</a>

<?php endforeach; ?>