• Resolved thisisedie

    (@thisisedie)


    I’m using this code in my functions.php to exclude posts from the front page and archives:

    function exclude_category($query) {
    if ( $query->is_archive || $query->is_home) {
    $query->set('cat', '-18 -20 -24 -26');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category');

    18 is a parent category and 20 and 24 are children of that category. I’d like to exclude all children in category 18 because I’ll be adding categories to that parent and don’t want to manually add them every time I do. I’ve tried everything I can think of using child_of and can’t make it happen. Any suggestions would be appreciated.

    PS: Same goes for the smartarchives plugin. I’ve tried everything I can think of on this line:

    $catID='18'; // the category(ies) you do *not* want to be displayed in your archives

    and nada :\

Viewing 15 replies - 1 through 15 (of 15 total)
  • I wrote up a little function that should work for you. There is probably a better way, but this was the first method that popped into my head.

    Add in this function:

    function excluded_child_cats($id) {
    	$ex_cats = get_categories('child_of=' . $id);
    	$result = ' -' . $id;
    	foreach ($ex_cats as $ec) {
    		$result .= ' -' . $ec->cat_ID;
    	}
    	return $result . ' ';
    }

    Simply supply this function with the ID of a parent cat, and it will return a string with a list of the sub-cats (with minus signs in front of them), including the parent cat.

    In other words:

    excluded_child_cats(18)

    Will return this string:

    ” -18 -20 -24 “

    So say you want to exclude the following pages:

    1 2 3

    And also all sub-pages of cats 10 and 11, you could do this (in the code you have above):

    $excluded_cats = '-1 -2 -3 ' . excluded_child_cats(10) . excluded_child_cats(11);
    $query->set('cat', $excluded_cats);

    Make sense?

    (As I said, there are other ways to write this, but you get the idea)

    Thread Starter thisisedie

    (@thisisedie)

    Thanks so much for your response! I’m probably doing something wrong (I’ve been staring at this stuff for hours, heh) but I keep getting errors. If I understand right I’d use:

    function excluded_child_cats('18') {
    	$ex_cats = get_categories('child_of=' . $id);
    	$result = ' -' . $id;
    	foreach ($ex_cats as $ec) {
    		$result .= ' -' . $ec->cat_ID;
    	}
    	return $result . ' ';
    }

    to exclude all the children of parent category 18? But when I do that I get errors *cries*

    Yes, you are doing something wrong ?? Its ok though.

    You do not need to make any changes at all to the function I gave you. It simply defines the function. You specify the value when using it. Like this:

    excluded_child_cats(10)

    Thread Starter thisisedie

    (@thisisedie)

    Ok, I think I’m not understanding, lol. The code I posted in the first post I have in my themes function.php and it excludes those categories from the home and archives pages. I thought you meant I could use:

    function excluded_child_cats(18) {
    $ex_cats = get_categories('child_of=' . $id);
    $result = ' -' . $id;
    foreach ($ex_cats as $ec) {
    $result .= ' -' . $ec->cat_ID;
    }
    return $result . ' ';
    }

    instead of my original code in the function.php to automatically exclude the children of category 18? But it gives me this error when i do that:

    Parse error: syntax error, unexpected T_LNUMBER, expecting ‘&’ or T_VARIABLE in /home/edie/public_html/blog/wp-content/themes/blog/functions.php on line 22

    Thanks for your help. I have a feeling I’m just not wrapping my brain around what you’re trying to tell me. Uggg.

    Sorry, it is difficult to explain if you are not familiar with PHP functions, but that block of code I showed you above:

    function excluded_child_cats($id) {
    	$ex_cats = get_categories('child_of=' . $id);
    	$result = ' -' . $id;
    	foreach ($ex_cats as $ec) {
    		$result .= ' -' . $ec->cat_ID;
    	}
    	return $result . ' ';
    }

    Does not need to be changed. By replacing $id with ’18’ the function no longer knows what the $id variable is.

    So to actually use that function, you simply call it like this:

    excluded_child_cats(parent category id)

    All the function does is generate a string like this: ‘ -1 -2 -3 ‘ with the children of the category you specify.

    So when I showed you the usage example:

    $excluded_cats = '-1 -2 -3 ' . excluded_child_cats(10) . excluded_child_cats(11);
    $query->set('cat', $excluded_cats);

    Assuming cat 10 had the children 20, 21, and 22, and cat 11 had children 30, 31, 32, the string generated would be:

    '-1 -2 -3 -10 -20 -21 -22 -11 -30 -31 -32'

    So it will do this:

    $query->set('cat', '-1 -2 -3 -10 -20 -21 -22 -11 -30 -31 -32');

    Thread Starter thisisedie

    (@thisisedie)

    Sorry to be so dumb about this. Sheesh! But where do I put:

    excluded_child_cats(18)

    ?

    I’ve been making themes for years but this is my first time digging down into the guts of things.

    I keep showing you, but I will put it in context.

    In the code from your first post, replace this:

    $query->set('cat', $excluded_cats);

    With this:

    $excluded_cats = '-1 -2 -3 ' . excluded_child_cats(10) . excluded_child_cats(11);
    $query->set('cat', $excluded_cats);

    Where 1 2 3 are cats you want to exclude, and 10 and 11 are cats you want to exclude (children also).

    How about this, give me the list of categories you want to exclude, and I will show you the code.

    Thread Starter thisisedie

    (@thisisedie)

    Ok I see why I was confused, lol. All I want to do is automatically exclude all the children of category 18.

    And category 18 itself too? Or just its children?

    Thread Starter thisisedie

    (@thisisedie)

    Just its children.

    Ok, replace the code in your original post with the code below. It will exclude all child cats of 18 without you having to do anything else if you add more categories.

    function excluded_child_cats($id) {
    // generates formatted string of sub cats
    	$ex_cats = get_categories('child_of=' . $id);
    	$result = '';
    	foreach ($ex_cats as $ec) {
    		$result .= ' -' . $ec->cat_ID;
    	}
    	return $result . ' ';
    }
    
    function exclude_category($query) {
    	if ( $query->is_archive || $query->is_home) {
    		$excluded_cats = excluded_child_cats(18);
    		$query->set('cat', $excluded_cats);
    	}
    	return $query;
    }
    
    add_filter('pre_get_posts', 'exclude_category');
    Thread Starter thisisedie

    (@thisisedie)

    OMG thank you so much. I see now. Usually I’m not this dumb about this kinda stuff. THANK YOU! Worked perfectly. *internet hug*

    Glad it worked for you. I know what its like to stare at code for a long time ?? Sometimes it just takes stepping back and looking at it from another direction.

    Btw, if you do decide to block more sub-categories, you can just call the function again, and combine the strings.

    For example, replace this line:

    $excluded_cats = excluded_child_cats(18);

    With this:

    $excluded_cats = excluded_child_cats(18) . excluded_child_cats(19);

    The two function calls generate the 2 lists, and the period character in PHP combines strings.

    Enjoy!

    Thread Starter thisisedie

    (@thisisedie)

    Yup, I actually just did that because I decided to change things up. Thank you again so much. And thank you for being so patient. I’ve been learning all kinds of new things this week ??

    Please tell me how to exclude only one or two child categories not all?

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Exclude Categories In Parent?’ is closed to new replies.