I want to display the links to all posts of the same category of the current post on that page. I wrote the following code in the single.php file.
<?php $categories_of_this_post = get_the_category(); ?>
<?php $fist_category_id = $categories_of_this_post['0']->term_id; ?>
<?php $this_post_ID = get_the_ID(); ?>
<div class="related-posts">
<ol>
<?php query_posts( array ( 'cat' => $fist_category_id, 'orderby' => 'date', 'order' => 'ASC' ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<?php if (get_the_ID() != $this_post_ID) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php else : ?>
<?php the_title(); ?>
<?php endif; ?>
</li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no articles written under this category.'); ?></p>
<?php endif; ?>
</ol>
</div>
But this code returns all the posts found on the WordPress website. Where it goes wrong?
get_posts()instead. – kaiser Jul 4 '12 at 11:16