jeudi 26 février 2015

excluding current post from get_posts


i have this block of code displayed in single.php template



<ul class="cat-wrap">
<?php
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
?>
<h5>same from <?php echo $category->name; ?></h5>
<?php
$args = array(
'numberposts' => 5,
'category' => $category->term_id,
'post__not_in' => array( $post->ID )
);
$posts = get_posts($args);
foreach($posts as $post) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
<li><a href="<?php echo get_category_link($category->term_id);?>" title="View all posts filed under <?php echo $category->name; ?>">جميع مقالات قسم <?php echo $category->name; ?></a></li>
<?php endforeach; ?>
</ul>


the loop is working fine except 'post__not_in' => array( $post->ID ) behaving in a weird way . to explain more if i have a post that share 4 different categories . the generated code is the following



<ul>
<h5>same from Cat1</h5>
<li>another article 1</li>
<li>another article 2</li>
<li>another article 3</li>
<li>another article 4</li>

<h5>same from Cat2</h5>
<li>Current Article</li>
<li>another article 1</li>
<li>another article 2</li>
<li>another article 3</li>

<h5>same from Cat3</h5>
<li>Current Article</li>
<li>another article 1</li>
<li>another article 2</li>
<li>another article 3</li>

<h5>same from Cat4</h5>
<li>Current Article</li>
<li>another article 1</li>
<li>another article 2</li>
<li>another article 3</li>


like you can notice only <h5>same from Cat1</h5> that excluding the current post , the rest including it and excluding some other random post.


thanks in advance and i hope i managed to explained my problem.





Aucun commentaire:

Enregistrer un commentaire