WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add list of category posts to sidebar (5 posts)

  1. eveums
    Member
    Posted 4 years ago #

    Hello,
    I would like to add a dynamic list of posts by category to my sidebar. For example, if I am viewing a single post from category "2", the sidebar would display links to all posts in category "2".

    How would I do this?

    thanks!

  2. MichaelH
    Member
    Posted 4 years ago #

  3. eveums
    Member
    Posted 4 years ago #

    Thank you! Truly, effective searching is an art! :)

    This code worked for me:

    <?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'=>5,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'More';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?
    
    ></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      } //if ($cats)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
  4. zOmbiesundaYs
    Member
    Posted 4 years ago #

    Thanks for the code snippet eveums!

  5. Ruriko
    Member
    Posted 4 years ago #

    How can I make it into a drop down menu?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.