WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query_posts displays wrong number of posts (9 posts)

  1. esdali
    Member
    Posted 2 years ago #

    Disclaimer: I don't know PHP! That being said, link to the code.

    Basically, for my categories, I tried to simplify it by, instead of making an individual page for each category (ie, category-1.php, category-2.php, etc), I used a lot of if and elseif statements to check for the current category, and to display the corresponding posts.

    • I have one div, that displays 1 "featured" post. This works correctly.
    • The second div checks the first post, to make sure it doesn't duplicate, and then is supposed to display 5 thumbnails that link to the post. It doesn't. It only displays 4.
      (That's why category one has "posts_per_page=6". I played around with the code and found out that it displays one less than it has to.)
    • The third div also checks for duplicates, and displays correctly (probably because I didn't have posts_per_page).

    The thing that kills me is that the home page, which is basically the same code sans the if and elseif statements, works just fine. So.. Could someone show me where I went wrong?

    Please and thank you!

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

  3. Digital Raindrops
    Member
    Posted 2 years ago #

    If this is the category or achive page, as I read it is, use the global $cat to find the $catid then work with that!

    global $cat;
    $curr_cat = get_category($cat);
    $catid = $curr_cat->cat_ID;
    $args=array(
       'cat' => $catid,
       'posts_per_page' => 1,
    );
    
    query_posts($args);

    HTH

    David

  4. Digital Raindrops
    Member
    Posted 2 years ago #

    Brain is working better today, $cat is a global that already holds the category -> ID so the extra lines can go, archive.php or category.php

    global $cat;
    $args=array(
       'cat' => $cat,
       'posts_per_page' => 1,
    );
    
    query_posts($args);

    HTH

    David

  5. esdali
    Member
    Posted 2 years ago #

    David,
    Here's the updated code. Unfortunately, the problem did not go away. I actually discovered that it's a site-wide problem, so I've posted my index.php.

    This conspiracy runs all the way to the top, I'm afraid. Something weird happens when I change the posts_per_page number. You see, I only have two posts with thumbnails currently, and they display ONLY when posts_per_page is set to 5 or more. When I put in 4, only one thumbnail is displayed, and the other thumbnail post is linked to in the third div. None of the other links, that are supposed to be displayed there, aren't there. The smaller the posts_per_page number in the second div, the more links show up in the third. It's an inverse relationship that I can't figure out.

    Any insights?

  6. Digital Raindrops
    Member
    Posted 2 years ago #

    Makes perfect sense, as you have five posts and skippng duplicates, you need to return the query like your code removing the duplicates in the arguments, and not skipping them in the loop!

    Use this argument and it should be ok:

    'post__not_in' => array( 2, 5, 12, 14, 20 )

    So you would use!

    <?php $img_args=array(
             'cat'=> -27,
             'post__not_in' => $removeDuplicates,
    	'posts_per_page' => 5,
    );

    HTH

    David

  7. esdali
    Member
    Posted 2 years ago #

    Okay, I'd hate to spam my own thread, but I have some updated information.

    I was playing around with the code a little more, and I realized that what was happening was that wordpress was putting articles that didn't meet the requirement of the query in the code, but with no link. As in:

    <a href="http://linkremoved.com/infinite-wisdom/article/whatever/" title="Text Removed">
          		      	</a>

    The weird thing is, if I change posts_per_page to two, then only one thumbnail appears, and the other becomes a silent link, even if it does meet the query_post requirement.

    Why does this happen?

  8. Digital Raindrops
    Member
    Posted 2 years ago #

    Wrong arguments, see my updated pastebin code!

    I have updated the code to see if the post has a thumbnail in section 2, and added a counter to get 5 thumbnail posts!

    If this resolves the issue mark it as resolved please!

    David

  9. esdali
    Member
    Posted 2 years ago #

    Thank you, thank you, thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic