• Hi,

    I am trying to list all post titles within a certain category in the sidebar. For example, i have 4 category headings listed down the left, and under each one i wish to display the post titles only that have been checked in that particular category.

    See Below

    Category 1
    – Category 1 Post 1
    – Category 1 Post 2
    Category 2
    – Category 2 Post 1
    Category 3
    – Category 3 Post 1
    Category 4
    – Category 4 Post 1

    I have done something similar before with listing child pages within a main page but not with listing posts under a particular category. Is this possible?

    Thanks in advance.

    Al

Viewing 3 replies - 1 through 3 (of 3 total)
  • Something like this?

    <?php
    //get categories, if category has posts, get a post in that category and display it
    $categories=get_categories();
    if ($categories) {
      foreach($categories as $category) {
        if ($category->count > 0) {
          echo '<p>Category <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> has ' . $category->count . ' post(s). </p> ';
          $number_posts_per_category = 1; // set = -1 to see all posts in the category
          $posts=get_posts('cat='. $category->term_id .'&showposts='.$number_posts_per_category);
          foreach($posts as $post) {
            setup_postdata($post); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php the_content();
          } // foreach ($posts
        } // if ($category->count > 0
      } // foreach ($categories
    } // if ($categories
    ?>
    Thread Starter ajkendall

    (@ajkendall)

    That is kind of what i am looking for and on the right lines but it doesn’t give me much control over what i want to display. I have edited the code to just display titles of each post but i need to display titles from only a particular category that i specify. For example, i want to display 3 posts under one category and none under another category, whereas, this code displays all posts in all categories. Thanks for your help so far, very much appreciated.

    You’ll have to use some if logic and change the $number_posts_per_category value based on your special categories.

    Also get_categories('include=3,5') could be specified to get category ids 3 and 5.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to list posts within a certain category’ is closed to new replies.