Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Showing all posts in Category in sidebar WordPress

Hi, I have nearly achieved what i'm after but I think I need help to tweak the code. I hope you can help!

When on a single post (single.php) I have a custom sidebar (below is the code) this pulls all the posts in that category and displays them in the sidebar, however it doesn't show the post that we are currently looking at.

This is a screenshot of the page using the code at the moment: http://www.testing-websites.com/image-dump/edit1.jpg if Viral Film 2 is the post I would like to show 'Viral film' & 'Viral Film 2' in the sidebar like this: http://www.testing-websites.com/image-dump/edit2.jpg

But at present it shows just the 'other' posts in the category, I want it to show 'all' the posts in the category.

Can anyone suggest a tweak to this code that will allow that?

Thanks
Rosco


<!--BEGIN #sidebar .aside-->
<div id="sidebar" class="aside">

<?php
if ( is_single() ) {
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$first_cat = $cats[0];
$args=array(
'cat' => $first_cat,
'post__not_in' => array($post->ID),
'showposts'=>20,
'caller_get_posts'=>0
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div id="archive-posts">
<div class="post-container clearfix">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { /* if post has post thumbnail */ ?>
<div class="post-thumb">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf(__('%s', 'framework'), get_the_title()); ?>"><?php the_post_thumbnail('thumbnail-large'); /* post thumbnail settings configured in functions.php */ ?></a>
</div>
<?php } ?>

<h2 class="entry-title1"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf(__('%s', 'framework'), get_the_title()); ?>"> <?php the_title(); ?></a></h2>


<!-- END .post-container -->
</div>
</div>

<?php
endwhile;
} //if ($my_query)
} //if ($cats)
wp_reset_query(); // Restore global post data stomped by the_post().
} //if (is_single())
?>


<!--END #sidebar .aside-->
</div>

Answers (3)

2011-06-20

Utkarsh Kukreti answers:

Just remove this line?

'post__not_in' => array($post->ID),


Ross Gosling comments:

Wow that was quick! Thank you Utkarsh that worked, I will award you the fee when I can!

2011-06-20

Navjot Singh answers:

Remove the following line and try:

'post__not_in' => array($post->ID),

2011-06-20

Fahad Murtaza answers:

Use wp_reset_query(); before your loop. Looks like its happening because of more than one loops on the single.php



<!--BEGIN #sidebar .aside-->

<div id="sidebar" class="aside">



<?php
wp_reset_query();
if ( is_single() ) {

$cats = wp_get_post_categories($post->ID);

if ($cats) {

$first_cat = $cats[0];

$args=array(

'cat' => $first_cat,

'post__not_in' => array($post->ID),

'showposts'=>20,

'caller_get_posts'=>0

);

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

while ($my_query->have_posts()) : $my_query->the_post(); ?>

<div id="archive-posts">

<div class="post-container clearfix">

<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { /* if post has post thumbnail */ ?>

<div class="post-thumb">

<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf(__('%s', 'framework'), get_the_title()); ?>"><?php the_post_thumbnail('thumbnail-large'); /* post thumbnail settings configured in functions.php */ ?></a>

</div>

<?php } ?>



<h2 class="entry-title1"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf(__('%s', 'framework'), get_the_title()); ?>"> <?php the_title(); ?></a></h2>





<!-- END .post-container -->

</div>

</div>



<?php

endwhile;

} //if ($my_query)

} //if ($cats)

wp_reset_query(); // Restore global post data stomped by the_post().

} //if (is_single())

?>





<!--END #sidebar .aside-->

</div>