• Hello

    On my home page i want to display my latest 5 pages OR posts, with an exception of posts from category 3.

    I use this code:

    <?php
    $postslist = get_posts('post_type&numberposts=5&offset=2');
     foreach ($postslist as $post) : setup_postdata($post);
    ?>  
    
    <?php if (in_category('3')) continue; ?>
    
     <div id="posti">
    
    <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>  
    
    <?php the_content_limit(150,'Ve? &rarr;'); ?>
    
     </div>
    
     <?php endforeach; ?>

    But the problem is, when in the last 5 posts there is one from category 3, the code shows just 4 posts.
    Do you have any idea how to do this, that it will always display 5 posts, no matter if there are any from category 3?

    Thanks in advance for your time, Marin

Viewing 9 replies - 1 through 9 (of 9 total)
  • if you had looked up the get_posts reference information in the codex, you would have found the ‘exclude’ parameter.

    https://codex.www.ads-software.com/Template_Tags/get_posts

    enjoy ??

    Thread Starter medak

    (@medak)

    Hey

    I looked. but $exclude is just for a specific ID, not for a category.
    Am i wrong?

    Ahh no you’re not *I’m* wrong. Sorry.

    use query_posts() to create a custom loop instead https://codex.www.ads-software.com/Template_Tags/query_posts

    this one *does* allow you to exclude categories.

    Thread Starter medak

    (@medak)

    I looked at query_posts, but i tried almost everything i could, but i didn’t manage to do it right. It displays or pages or posts, but not pages and posts.

    I spend last 2 hours looking for the solution to display pages and posts at the same time, but i didn’t find it ??

    Do you have any idea?

    well, I don’t understand how your pages are supposed to work in the chronology.

    do you create new pages all the time? if so, why the hell would you do that when you can just post into a new category?

    pages and posts are almost identical, until that ‘almost’ part comes to bite you in the ass at display time.

    Thread Starter medak

    (@medak)

    ??
    Unfortunately yes, i can not use categories for this purpose.

    And yes, that ‘almost’ is a big pain in the ass ??

    ps: pozdrav u Austrailiju

    OK… since there seems to be no wordpress function which will do exactly what you want, you have 2 choices.

    1) write your own SQL query, which is effective, but not very future-resistant. It would be best to use wordpress functions.

    2) use wordpress functions but cheat a little. It’s not going to win you any programming awards, but it’ll work.

    consider pulling more results than you need, but only outputting the first 5. This should compensate for any posts in the category you wish to exclude.

    lets try this:

    <?php
      $count = 1;
      $postslist = get_posts('post_type&numberposts=10&offset=2');
      foreach ($postslist as $post) :
        setup_postdata($post);
        if (!in_category('3') && $count < 6) :
    ?>
          <div id="post-<?php echo $count++; ?>" class="posti">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
            <?php the_content_limit(150,'Ve? &rarr;'); ?>
          </div>
    <?php
        endif;
      endforeach;
    ?>

    puno sre?e.

    Thread Starter medak

    (@medak)

    Thank you very much!
    The code works great, just now ther is a problem with offset ??
    Look at this example(1. is last posted, 2. is second last posted…:
    1.postZZZ-(published in category 3)
    2.postXXX-(not published in category 3)
    3.postYYY-(not published in category 3)
    4.postMMM-(not published in category 3)

    If offset=2, the first post to apper it will postYYY. But i need to display the postMMM. Offset in my situacion is relativ (i want to skip first two posts that are not from category 3, but the first two latest posts). Do you know what i mean? ??

    Hi,
    I was having a similar problem. I ended up using

    query_posts("cat=-X")

    where X is the category you want to exclude. I found that this didn’t interfere with the count in the loop and it now shows the appropriate number of posts excluding the category I wanted hidden.

    I just placed it in my code before the

    if (have_posts()) :

    Codex: query_posts > Category Parameters

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘please help with get_posts()’ is closed to new replies.