J'ai une boucle qui devrait théoriquement afficher 4 messages liés - par tag en premier lieu ET quand il n'y a pas assez de messages liés aux tags, cela devrait prendre les messages restants de la même catégorie.
Le code fonctionne bien lorsqu'au moins une étiquette correspond, les 3 autres postes sont issus de la même catégorie. Mais s'il n'y a pas de messages liés au tag, la boucle ne prend pas les 4 messages de la même catégorie. Il montre simplement un contenu totalement indépendant de la catégorie différente.
<?php
$max_articles = 4; // How many articles to display
echo ' <div id="carousel-related-posts" class="owl-carousel owl-theme relatedposts">';
$cnt = 0;
// Show articles based on the same tag
$article_tags = get_the_tags();
$tags_string = '';
if ($article_tags) {
foreach ($article_tags as $article_tag) {
$tags_string .= $article_tag->slug . ',';
}
}
$tag_related_posts = get_posts('exclude=' . $post->ID . '&numberposts=' . $max_articles . '&tag=' . $tags_string);
if ($tag_related_posts) {
foreach ($tag_related_posts as $related_post) {
$cnt++;
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($related_post->ID), 'medium_large' );?>
<div class="header-wrap" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat 50% 30%; background-size: cover;">
<header class="entry-header">
<div class="image-title">
<?php incomplete_cat_list($related_post->ID); ?>
<a class="post-title-link" href="<?php the_permalink($related_post->ID); ?>">
<?php print get_the_title($related_post->ID); ?>
</a>
</div>
</header>
</div>
<?php }
}
// Only if there's not enough tag related articles,
// add some from the same category
if ($cnt < $max_articles) {
$article_categories = get_the_category($post->ID);
$category_string = '';
foreach($article_categories as $category) {
$category_string .= $category->cat_ID . ',';
}
$cat_related_posts = get_posts('exclude=' . $post->ID . '&numberposts=' . $max_articles . '&category=' . $category_string);
if ($cat_related_posts) {
foreach ($cat_related_posts as $related_post) {
$cnt++;
if ($cnt > $max_articles) break;
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($related_post->ID), 'medium_large' );?>
<div class="header-wrap" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat 50% 30%; background-size: cover;">
<header class="entry-header">
<div class="image-title">
<?php incomplete_cat_list($related_post->ID); ?>
<a class="post-title-link" href="<?php the_permalink($related_post->ID); ?>">
<?php print get_the_title($related_post->ID); ?>
</a>
</div>
</header>
</div>
<?php }
}
}
echo '</div>';
?>
Toute idée pourquoi ce code ne prend pas tous les messages liés de la même catégorie, quand il ne trouve pas au moins un tag lié au poste?
Vous n'avez pas expliqué le cas où aucune balise n'a été attribuée dans votre déclaration de $ tag_related_posts. Votre fonction get_posts renvoie toujours les résultats dans votre ligne de code:
$tag_related_posts = get_posts('exclude=' . $post->ID . '&numberposts=' . $max_articles . '&tag=' . $tags_string);
Si $ tags_string a la valeur null, il renvoie simplement une liste aléatoire de publications et remplit toujours le nombre maximal de publications. Ainsi, votre condition requise pour accéder à la catégorie ne se déclenche jamais.
Si vous déplacez votre clôture} pour if($article_tags)
immédiatement après la boucle foreach
vers après le bloc if($tag_related_posts)
, la fonctionnalité souhaitée apparaît:
<?php
$max_articles = 4; // How many articles to display
echo ' <div id="carousel-related-posts" class="owl-carousel owl-theme relatedposts">';
$cnt = 0;
// Show articles based on the same tag
$article_tags = get_the_tags();
$tags_string = '';
if ($article_tags) {
foreach ($article_tags as $article_tag) {
$tags_string .= $article_tag->slug . ',';
}
$tag_related_posts = get_posts('exclude=' . $post->ID . '&numberposts=' . $max_articles . '&tag=' . $tags_string);
if ($tag_related_posts) {
foreach ($tag_related_posts as $related_post) {
$cnt++;
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($related_post->ID), 'medium_large' );?>
<div class="header-wrap" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat 50% 30%; background-size: cover;">
<header class="entry-header">
<div class="image-title">
<?php incomplete_cat_list($related_post->ID); ?>
<a class="post-title-link" href="<?php the_permalink($related_post->ID); ?>">
<?php print get_the_title($related_post->ID); ?>
</a>
</div>
</header>
</div>
<?php } //end of foreach($article_tags as $article_tag)
} //end of if($tag_related_posts)
} //end of if($article_tags)
// Only if there's not enough tag related articles,
// add some from the same category
if ($cnt < $max_articles) {
$article_categories = get_the_category($post->ID);
$category_string = '';
foreach($article_categories as $category) {
$category_string .= $category->cat_ID . ',';
}
$cat_related_posts = get_posts('exclude=' . $post->ID . '&numberposts=' . $max_articles . '&category=' . $category_string);
if ($cat_related_posts) {
foreach ($cat_related_posts as $related_post) {
$cnt++;
if ($cnt > $max_articles) break;
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($related_post->ID), 'medium_large' );?>
<div class="header-wrap" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat 50% 30%; background-size: cover;">
<header class="entry-header">
<div class="image-title">
<?php incomplete_cat_list($related_post->ID); ?>
<a class="post-title-link" href="<?php the_permalink($related_post->ID); ?>">
<?php print get_the_title($related_post->ID); ?>
</a>
</div>
</header>
</div>
<?php }
}
}
echo '</div>';
?>