WordPress.org

Ready to get started?Download WordPress

Forums

Displaying Category Posts (5 posts)

  1. subtype
    Member
    Posted 6 years ago #

    I know this is simple, but when i am looking at single.php and an individual post, i want to display the most recent 5 posts in that category. But it obviously has to be dynamic so it knows which category your in, how do i do this?

    I have this and it doesn't work; it displays posts from all categories:

    $cat = get_query_var('cat');
    global $post;
    $myposts = get_posts('numberposts=5&offset=1&category=$cat');

  2. subtype
    Member
    Posted 6 years ago #

    anyone? anyone at all?

  3. SimonJ
    Member
    Posted 6 years ago #

  4. subtype
    Member
    Posted 6 years ago #

    That doesn't work - I tried get_the_category(); with no success.

  5. SimonJ
    Member
    Posted 6 years ago #

    Well... You can't just use only the get_the_category(); tag, alone, as is...

    You have to build a loop with the result :

    <?php $cat = get_the_category(); $cat = $cat[0]; ?>
    <?php
    query_posts(array(
    'cat'=> $cat->cat_ID,
    'showposts'=>'5',
    ) );
    ?>
    <?php while (have_posts()) : the_post(); ?>
    --- DO YOUR STUFF (list titles, authors, date, etc ---
    <?php endwhile; ?>

    So yes, it works... ;-)

    S.

Topic Closed

This topic has been closed to new replies.

About this Topic