WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've done some research and tried different solutions but this snippet gets me the actual post content in sidebar, when on a single/article page, from the category the post belogs to.

But I need to show the excerpt instead of the whole text and also a thumbnail and can't figure out how to add them.

Any suggestions please?

$categories = get_the_category();
echo '<ul><li>';
foreach ($categories as $c) {
    if (strpos(get_permalink($post->ID), $c->slug) !== false) {
        echo '<a href="/' . $c->slug . '/">' . $c->name . '</a>';
        $current_cat = $c->cat_ID;
$posts = get_posts(array('cat' => $current_cat));
if (!empty($posts)) {
    echo '<ul>';
    foreach ($posts as $p) {
        echo '<li><a href="' . get_permalink($p->ID) . '">' . $p->post_title . '</a></li>';
        echo $p->post_content;
    echo '</ul>';
echo '</li></ul>';

Or another approach maybe? Thank you.

share|improve this question

I tried using your code and fixing it up but it wasn't having any luck. Try this out and let me know how it works for you:

  $args = array( 
    'numberposts' => 3, 
    'order'=> 'DESC', 'orderby' => 'post_date', 
    'category' => '' 
  $postslist = get_posts( $args );
  foreach ($postslist as $post) :  setup_postdata($post);
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?>
    <?php the_post_thumbnail('thumbnail'); ?></a>
<?php the_excerpt(); ?>
<?php endforeach; ?>
share|improve this answer
Thank you Jeremy, and sorry for my late reply. Well, yours is displaying me the thumb, title and excerpt but the title and the photo is the same, is just the excerpt which is changed accordingly. I think that means that the function is used well, the args are alright but the foreach is not functioning as it should, right? – C D Dec 28 '11 at 12:04

Try plugin Mini Loops. It has the features you need. Also checkboxes "Get posts from current category (if archive)?" and "Get posts from first category (if single)?".

share|improve this answer
That looks fantastic, will use it next time. I have now lost way too much time on this and not only that, but I would really like to understand the problem properly so will try and fix my code. Thanks! – C D Dec 28 '11 at 12:08
up vote 0 down vote accepted

I have found a solution and modified it for my needs, maybe there are people looking for same thing, so there you go:

if ( is_single() ) {
  $categories = get_the_category();
  if ($categories) {
    foreach ($categories as $category) {
      $cat = $category->cat_ID;
        'cat' => $cat,
        'post__not_in' => array($post->ID),
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo '<h2>More in this section</h2>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <div class="cat-related-articles">
<?php the_post_thumbnail('thumb'); ?>          

<p class="sidebar-article-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link 

to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

<?php the_excerpt(5); ?>  

      } //if ($my_query)
    } //foreach ($categories
  } //if ($categories)
  wp_reset_query();  // Restore global post data stomped by the_post().
} //if (is_single())

Now, my problem is:


They don't work. I am trying to leave out and not display the post I am currently on. Remember, if I click on an article article, this code in the sidebar would display the title, thumb and excerpt of 3 more articles from the same category.

So, how do I make it not to display the post I am currently viewing?

Another thing, I the article I am on belongs to 2 categories, that code will show me posts from each category. How do I get rid of this?!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.