WordPress.org

Ready to get started?Download WordPress

Forums

Showing post titles under each Category (5 posts)

  1. kuran
    Member
    Posted 3 years ago #

    I'm new to WordPress, tried searching but was overwhelmed with older posts, and unanswered threads.

    I would like to show a simple list of posts (titles only) underneath each category, how should I tackle this?

    An example:

    Category 1
    Posting a
    Posting b

    Category 2
    Posting d

    Category 3
    Posting e

    I appreciate any information.

  2. Tara
    Member
    Posted 3 years ago #

    see this thread for suggestion:

  3. Michelle Langston
    Theme Wrangler at Automattic
    Posted 3 years ago #

    Do you want this on the front page, such as in a magazine or newspaper site? Or are you trying to build a category archive page?

    For a magazine/news style site, you could achieve this using multiple loops: http://codex.wordpress.org/The_Loop#Multiple_Loops

    For a simple category archive, you could try this plugin: http://www.utechworld.com/projects/list-posts-for-wp/

    If you decide not to use the plugin and want to dive into some PHP, it would be something like this (I haven't tested it so I'm not sure it'll work but try it), I'd be happy to modify it:

    <?php
    $cats = get_categories();
    foreach ($cats as $cat) {
    <p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    
    query_posts('cat='.$cat->term_id.'&showposts=5');
    
    while(have_posts()) : the_post();
    echo '<ul>'; ?>
    
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; 
    
    echo '</ul>'
    }
    ?>

    You would place that code into a page template, after "endif;". Watch the <?php and ?> tags.

  4. kuran
    Member
    Posted 3 years ago #

    Thank you for your detailed reply, chellycat!

    I want it in place, or in addition to this line in the sidebar of my blog. (sidebar.php)

    <?php wp_list_categories('show_count=1&title_li='); ?>

    So it isn't for the archive page, it should be visible on most pages.

  5. Michelle Langston
    Theme Wrangler at Automattic
    Posted 3 years ago #

    Ah, I see an error in the code I posted above, use this version instead (if you do decide to use it):

    <?php
    $cats = get_categories();
    foreach ($cats as $cat) {
    <p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    
    query_posts('cat='.$cat->term_id.'&showposts=5');
    
    echo '<ul>'; 
    
    while(have_posts()) : the_post();?>
    
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; 
    
    echo '</ul>'
    }
    ?>

    Try replacing <?php wp_list_categories('show_count=1&title_li='); ?> in your sidebar with this code and see what happens.

Topic Closed

This topic has been closed to new replies.

About this Topic