Forums

Show posts from current category within the main loop (3 posts)

  1. clee109
    Member
    Posted 1 year ago #

    I am not a PHP guru at all so I need alot of help here. I'd love for someone to post the code for this and also perhaps explain how it works, I'd really appreciate the answer and some teaching. Thanks everyone!

    What I want to do is this:
    In single view within the main loop return the 5 most recent posts from the category the current post is under. I understand to do this within the loop I need to use WP_Query as to not disrupt the loop. What I don't know is what args to use to extract the information.

    I've seen several posts on how to do this OUTSIDE of the loop but none from within the loop. I don't know how to take those args and put them into WP_Query and then have it place the links in a

  2. loop.

    Thanks for the help I really appreciate it!

  • esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    <?php
     $recentposts = get_posts('numberposts=5');
     foreach($recentposts as $post) :
        setup_postdata($post);
     ?>
     <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
     <?php the_content(); ?>
     <?php endforeach; ?>

    http://codex.wordpress.org/Function_Reference/get_posts

  • clee109
    Member
    Posted 1 year ago #

    Perfect, thank you!

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags