• Hi, I am hoping someone can help me with the basic loop code for an archive.php page (which would also function as the categories page). Ideally I’d like to just produce a list of post titles, or post titles with brief excerpts. Here’s my code — it brings up the first post under the first category, and nothing else:

    <?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
    ?>

    I am using permalinks, if that helps. I am still in the learning phase (obviously); many thanks in advance for any help offered.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Change this:

    <?php the_content();

    to

    <?php the_excerpt();

    Also review the first four questions here:
    http://codex.wordpress.org/FAQ_Layout_and_Design#Text_and_Content_Display

    [edited link]

    See Excerpt

    Thread Starter 2cats

    (@2cats)

    Thank you so much, that really helped. Now I have the problem that when I click on any category, it brings up all categories alphabetically with the post excerpts listed beneath each. How can I limit it strictly to the selected category? I went through the links you recommended (good information) but couldn’t find a way to limit it to the category selected by the user. (Lots of information about limiting it to a category that I select, though.) Thanks again.

    You probably need to review Template Hierarchy because if you coded your Category Template to show all categories that could be the problem.

    Switch to the WordPress Default theme and see how it handles category views.

    Also, if you are referring to child vs. parent categories that is a different issue.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘list posts under category’ is closed to new replies.