Support » Fixing WordPress » Displaying Category Posts

  • subtype

    (@subtype)


    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’);

Viewing 4 replies - 1 through 4 (of 4 total)
  • subtype

    (@subtype)

    anyone? anyone at all?

    SimonJ

    (@simonj)

    subtype

    (@subtype)

    That doesn’t work – I tried get_the_category(); with no success.

    SimonJ

    (@simonj)

    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.

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