Forums

[resolved] How do I limit the number of posts grabbed from a secondary loop? (4 posts)

  1. bobdobbs
    Member
    Posted 7 months ago #

    I've grabbed posts using an instance of WP_Query.
    I have the posts in $my_query. I have used them initially to populate a sidebar.

    Now I want to reuse the posts in $my_query. I have rewound it.

    How do I limit the new loop, so that only one post is produced?

  2. alchymyth
    The Sweeper
    Posted 7 months ago #

    possibly, add break; before the endwhile; of the second loop.

    details depend on your code - you can paste the full code into a http://pastebin.com/ and post the link to it here (how to, see http://codex.wordpress.org/Forum_Welcome#Posting_Code ), for somone to check this.

  3. Navaratnam
    Member
    Posted 7 months ago #

    declare a variable and limit it.
    $i = 0;
    WP Loop bigins
    if ($i < 1){
    Display the post
    }
    $i++;
    WP Loop ends

  4. bobdobbs
    Member
    Posted 7 months ago #

    Thanks for the help.

    Hacking away at this has helped me to get a bit more used to wp and php (and the potential frustrations of working with both of them)

    The solution I used was given to me on #wordpress by dc5ala. He suggested this:

    ($myquery->have_posts()) {
    	 $myquery->the_post(); the_title(); } wp_reset_postdata();

    This worked.

Reply

You must log in to post.

About this Topic

Tags