WordPress.org

Ready to get started?Download WordPress

Forums

ignore category (3 posts)

  1. RobotHero
    Member
    Posted 10 years ago #

    So, when I use this:
    $cat = '-3';
    it's supposed to ignore posts of category 3.
    But, say I have some posts that are of both category 3, and category 2. It still shows them. It only ignores posts that are only category 3.
    How would I make it ignore anything that is of category 3, even if it is also in another category?

  2. RobotHero
    Member
    Posted 10 years ago #

    Well, I've gone and done it the hard way.
    This goes in my-hacks:
    function the_category_IDs() {
    $IDs = array();
    $categories=get_the_category();
    foreach ($categories as $category)
    {
    $IDs[] = $category->category_id;
    };
    return $IDs;
    }
    function isthree($post) {
    return (in_array(3,the_category_IDs()));
    }
    function remove_posts($function) {
    global $posts, $post;
    $newposts = array ();
    if (stristr($function,'!'))
    {
    $function = explode('!',$function);
    $function = $function[1];
    foreach ($posts as $post) {
    if ($function($post))
    {
    $newposts[] = $post;
    }
    }
    } else {
    foreach ($posts as $post) {
    if (!$function($post))
    {
    $newposts[] = $post;
    }
    }
    }
    return $newposts;
    }

    the_category_IDs() is a function I had written before that returns an array of the categories of $post.
    remove_posts() takes a function as a parameter. This function should accept $post as a parameter, and return a boolean. Any post for which the function returns True will be removed from the array of posts returned by remove_posts(). If remove_posts() is passed the name of a function preceded by ! it will remove all posts for which the function returns False.
    isthree() is just a function that tests if a post is of category 3. You can replace it with whatever test you want to perform on your posts.
    Then, in my index.php, I include this immediately before the Loop:
    <?php $posts = remove_posts('isthree') ?>
    Note that removing posts this way will reduce the total number of posts. IE: if your posts_per_page is set to 5, and 3 of your posts are removed, then you will only display 2 posts. I'll see what I can do to improve that.

  3. RobotHero
    Member
    Posted 10 years ago #

    And now that I've got 1.2, that was pointless. (Other than the practice)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.