Support » Plugins » Help with hack to exclude category

  • Resolved dredub38

    (@dredub38)


    I’ve got this code in my sidebar.php file. It determines what categories have been assigned to the current post, and then shows 10 headlines from each category in a list. I need to figure out how to make it exclude a specific category number (not list the headlines from that category) and just display lists for the remaining categories.

    This is what I am working with.

    <?php
    // this is where 10 headlines from the current category get printed
    global $post;
    $categories = get_the_category();
    foreach ($categories as $category) :
    ?>
    <li><h2>The Latest From This Topic</h2>
    <ul class="bullets">
    <?php
    $posts = get_posts('numberposts=10&category='. $category->term_id);
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </li>
    <?php endforeach; ?>

    I hope I have given enough information here to explain what I’m trying to accomplish, and I appreciate any help you can offer.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter dredub38

    (@dredub38)

    Well it was worth a shot. 🙂

    edit this line with the category number you want to exclude
    $posts = get_posts('numberposts=10&category=-3'. $category->term_id);
    replace 3 with whatever your category number is.

    Thread Starter dredub38

    (@dredub38)

    That didn’t work. It actually still displays the multiple categories, but didn’t show the information (bullet points) under them.

    But I thank you for trying! 🙂

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    I need to figure out how to make it exclude a specific category number (not list the headlines from that category) and just display lists for the remaining categories.

    So, not display that category in the resulting lists? Do this:

    ...
    $categories = get_the_category();
    foreach ($categories as $category) :
    if ($category->term_id == 3) continue; // replace 3 with the cat id to exclude
    ...
    Thread Starter dredub38

    (@dredub38)

    Yes! It works wonderfully.

    Thank you Otto (and you as well boober).

    Just in case anybody else ever has a similar need, I’ll post the final bit of code. Works great to exclude 1 or more categories when showing the latest headlines from related categories in the sidebar.

    <?php
    // this is where 10 headlines from the current category get printed
    global $post;
    $categories = get_the_category();
    foreach ($categories as $category) :
    if ($category->term_id == 3) continue; // replace 3 with the cat id to excluded
    if ($category->term_id == 4) continue; // replace 4 with the cat id to excluded
    ?>
    <li><h2>More from this category</h2>
    <ul class="bullets">
    <?php
    $posts = get_posts('numberposts=10&category='. $category->term_id);
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endforeach; ?>
    
    <li><strong><a href="<?php echo get_category_link($category->term_id);?>" title="View all posts filed under <?php echo $category->name; ?>">Archive for '<?php echo $category->name; ?>' &raquo;</a></strong></li>
    </ul>
    </li>
    <?php endforeach; ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Help with hack to exclude category’ is closed to new replies.