Support » Themes and Templates » List Posts in Category

  • I found the following code on the forums:

    <?php $posts = get_posts( "category=10" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    	<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <?php endforeach; ?>
    <?php endif; ?>

    The goal is for it to list the latest 5 post titles on the category page that it is in.

    The way it is currently written, the category ID is hard coded in, how can I make it dynamic based on which category it loads?

    Thanks for any help anybody can provide

Viewing 4 replies - 1 through 4 (of 4 total)
  • You do NOT need that code for a simple category archive listing.

    Take a look at the archive.php file of the default theme – that does the job.
    As for the number of posts displayed… either go to your admin > Options > Reading and set the number (it applies to all views though!) or use a plugin like CQS

    p.s. If you want to make it fancy, read about the Category_Templates

    I will read through it, thanks for the fast reply. This has been bothering me for the last few hours.

    I looked into the links you provided, but the problem is that the code only allows me to pull a static category, how can I make it pull for the category archive that I am on?

    You don’t get it: clicking on a category name WP knows better than you what to show. It will display ONLY the posts from that category, using one of the templates according to the Template_Hierarchy.
    So, what is the question?
    Show me your WP installation that doesn’t do that by default!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘List Posts in Category’ is closed to new replies.