• I usually use something like this to show posts:

    <?php
    $current_category = single_cat_title("", false);
    echo $current_category;
    $lastposts = get_posts('numberposts=5&category_name='.$current_category);
    foreach($lastposts as $post) : setup_postdata($post);
    ?>

    Now, I have two loops in this category page. The first loop should display 3 important posts in that category (either by adding to it a subcategory like “Category TOP” or with a tag). The second loop should display all the other posts in that category.

    So, I can use this code for the first 3 posts
    numberposts=3&tag=top&category_name=’.$current_category to display only the posts that are in that category AND have the tag “top”… this is all good.

    BUT HOW do I exclude from the second loop the posts from the first one?
    I dont want duplicated posts, so Im thinking about something like numberposts=10&exclude_tag=top&category_name=’.$current_category but as the exclude_tag parameter doesnt exist… how do I create it?
    Or, if I used a subcategory to display the first 3 instead of a tag, I’d need a way in the second loop to exclude posts that are ALSO in the subcategory.

    I cant manually put category IDs as there are too many, that’s why I use current_category…
    I cant figure out a way to exclude from the second loop the posts from that I already showed in the first one…

    Can anyone help please?

Viewing 1 replies (of 1 total)
  • Something like:

    <?php
    $nodup = array();
    $current_category = single_cat_title("", false);
    echo $current_category;
    $lastposts = get_posts('numberposts=5&category_name='.$current_category);
    foreach($lastposts as $post) : setup_postdata($post);
    $nodup[]=$post->ID;
    //rest of your first loop here
    
    //now 2nd loop
    
        $args=array(
          'post__not_in' => $nodup,
          'tag__not_in' => array(156,345),
          'posts_per_page'=>10,
          'category_name=' => $current_category,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
    ?>

    Also see:
    query_posts() tag parameters

Viewing 1 replies (of 1 total)
  • The topic ‘get_posts to exclude posts from cats or tags dynamically’ is closed to new replies.