// ===== SHORTCODE [dernier_article] (paramétrable) =====
// Affiche N derniers articles (défaut : 1) avec extrait + bouton "Lire l'article complet"
if ( ! function_exists( 'bfrr_dernier_article_shortcode' ) ) {
function bfrr_dernier_article_shortcode( $atts = array() ) {
// Attributs : nb = nombre d'articles à afficher (défaut = 1)
$atts = shortcode_atts(
[
'nb' => 1,
],
$atts,
'dernier_article'
);
$nb = max( 1, intval( $atts['nb'] ) );
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $nb,
];
$query = new WP_Query( $args );
if ( ! $query->have_posts() ) {
return '<p>Aucun article trouvé.</p>';
}
ob_start();
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
$permalink = get_permalink();
echo '<article class="bfrr-dernier-article">';
// Titre cliquable
echo '<h2 class="bfrr-da-titre"><a href="' . esc_url( $permalink ) . '">' . esc_html( get_the_title() ) . '</a></h2>';
// Date
echo '<p class="bfrr-da-meta">Publié le ' . esc_html( get_the_date() ) . '</p>';
// Catégories
$cats_list = get_the_category_list( ', ' );
if ( $cats_list ) {
echo '<p class="bfrr-da-taxo bfrr-da-cats"><strong>Catégories : </strong>' . wp_kses_post( $cats_list ) . '</p>';
}
// Sources (taxonomie personnalisée)
$sources = get_the_terms( $post_id, 'sources' );
if ( $sources && ! is_wp_error( $sources ) ) {
$sources_names = wp_list_pluck( $sources, 'name' );
echo '<p class="bfrr-da-taxo bfrr-da-sources"><strong>Sources : </strong>' . esc_html( implode( ', ', $sources_names ) ) . '</p>';
}
// Étiquettes
$tags_list = get_the_tag_list( '', ', ' );
if ( $tags_list ) {
echo '<p class="bfrr-da-taxo bfrr-da-tags"><strong>Étiquettes : </strong>' . wp_kses_post( $tags_list ) . '</p>';
}
// 🔹 EXTRAI T
echo '<div class="bfrr-da-contenu">';
echo wp_kses_post( wp_trim_words( get_the_excerpt(), 55, '...' ) );
echo '</div>';
// 🔹 Bouton "Lire l'article complet"
echo '<p class="bfrr-da-bouton-wrapper">';
echo '<a class="bfrr-da-bouton" href="' . esc_url( $permalink ) . '">Lire l’article complet</a>';
echo '</p>';
echo '</article>';
}
wp_reset_postdata();
return ob_get_clean();
}
}
add_shortcode( 'dernier_article', 'bfrr_dernier_article_shortcode' );