List Posts in Category (5 posts)

  1. MC82
    Posted 8 years ago #

    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

  2. moshu
    Posted 8 years ago #

    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

  3. MC82
    Posted 8 years ago #

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

  4. MC82
    Posted 8 years ago #

    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?

  5. moshu
    Posted 8 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic