Remove duplicated posts in the loop if post has more than one category
-
07-11-2019 - |
Question
I have some posts with more than one category. In the post loop i get the post based on the selected category by the user. If the user select 2 categories where one post is assigned to, the loop duplicate the post. I tried to use array_unique()
but doesn't work.
Here is my code:
<?php
global $current_user;
get_currentuserinfo();
//$current_user->user_categories;
$ucategory = maybe_unserialize($current_user->user_categories);
foreach ($ucategory as $category) {
$query = new WP_Query( array( 'post_type' => array( 'service'),
'category__in' => array($category)));
$i=0;
while ( $query->have_posts() ) : $query->the_post();
if ($i%3==0) echo '
';
echo '
<div class="col-md-4 ">
<div class="col-md-12 service-cell">
<a href="'; the_permalink(); echo '">';
echo the_post_thumbnail('thumbnail', array('class' => 'app-icon'));
echo '<h2 class="center">'; the_title(); echo '</h2></a>';
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(3, $childcat)) {
echo '<h3 class="sub">';
echo $childcat->cat_name . '</h3>';
}}
echo '<p>';
echo excerpt(25);
echo '<p><a href="'; the_permalink(); echo '">'; echo 'Wijzig uw gegevens</a>
<a class="pull-right"><span class="label label-default"><span class="glyphicon glyphicon-ok"></span></span></a></p>';
echo '</p>';
echo '';
echo '</div></div>';
if ($i%3==2) echo '';
$i++;
endwhile;
}
?>
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange