logo

Articles connexes [PHP]

Il est parfois intéressant de proposer à ses visiteurs des articles en relation avec celui qu’ils sont en train de lire. Pas besoin de plugin pour cela. Rien de plus simple. Mettez le code suivant dans le fichier single.php de votre thème, là où bon vous semble. Nous avons laisser la mise en forme HTML CSS Bulma.

single.php

<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Articles connexes';
$first_tag = $tags[0]->term_id;
$args = array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page' => 6,
'caller_get_posts' => 1
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>

<div class="columns is-multiline is-centered has-text-centered">
<div class="column is-2 is-marge">
<article>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="card shadow">
<div class="card-image">
<?php if (has_post_thumbnail()) { ?>
<picture>
<?php the_post_thumbnail('thumbnail'); ?>
</picture>
<?php } else { ?>
<?php
$curdir = getcwd();
chdir(get_template_directory() . "/css/img/random/");
$files = glob("*.{gif,png,jpg,gif}", GLOB_BRACE);
chdir($curdir);
$file = $files[array_rand($files)];
?>
<picture>
<img src="<?php echo (get_bloginfo('template_url') . "/css/img/random/$file"); ?>" alt="<?php bloginfo('name'); ?>" />
</picture>
<?php } ?>
</div>
<div class="card-content">
<h4 class=" is-5">
<?php the_title(); ?>
</h4>
<div class="content">
<?php the_excerpt();  ?>
</div>
</div>
</div>
</a>
</article>
</div><!-- /column -->
</div>

<?php
endwhile;
}
wp_reset_query();
}
?>