Forum Replies Created

Viewing 8 replies - 16 through 23 (of 23 total)
  • Thread Starter musicmasteria

    (@musicmasteria)

    Thanks alanft,

    The code that I ended up using was

    is_single() && (post_is_in_descendant_category(5) || in_category(5))

    With this in my theme’s function.php code:

    function post_is_in_descendant_category( $cats, $_post = null )
    {
    	foreach ( (array) $cats as $cat ) {
    		// get_term_children() accepts integer ID only
    		$descendants = get_term_children( (int) $cat, 'category');
    		if ( $descendants && in_category( $descendants, $_post ) )
    			return true;
    	}
    	return false;
    }

    I am curious though, does it use more or less resources reading the function from the function.php file than reading it straight from the code like I had before?

    – – – – –
    – – – – –

    There is one thing I have been leaving out to simplify things though. It is that I’m actually using 2 widgets, 1 widget that appears only in the specific area and other widget that appears in all other places but not the specific area.

    The code I’ve been using for the second widget is:

    !(is_single() && (post_is_in_descendant_category('5') || in_category('5')))

    (I just did !(—-), where —- is the the entire first widget’s code.)

    Is this the right way of doing this or is there an easier way for doing that?

    Thanks again alanft,
    ~Musicmasteria

    Thread Starter musicmasteria

    (@musicmasteria)

    SORRY!
    I should have tested the code first, duh..

    The code should look like this:

    $category = get_the_category(); $parent = $category[0]->category_parent; return (is_single() && (('5'==$parent) || in_category('5')));

    The “1 Fix” above works but the “1 Edit” doesn’t work because “get_cat_id” doesn’t exists and actually, I already had the id and didn’t notice it. “$category[0]->category_parent” already returns the ID so I set it to equal $parent instead of “get_cat_name(____)”.

    I should have known better :/ Sorry.

    Thread Starter musicmasteria

    (@musicmasteria)

    I have seen that code before but when I tried it, I don’t think it worked.

    If I remember correctly it needed the code below somewhere to make post_is_in_descendant_category(5) work. As I have had very little luck with if’s and function’s in widget-logic code this code didn’t work for me.

    function post_is_in_descendant_category( $cats, $_post = null )
    {
    	foreach ( (array) $cats as $cat ) {
    		// get_term_children() accepts integer ID only
    		$descendants = get_term_children( (int) $cat, 'category');
    		if ( $descendants && in_category( $descendants, $_post ) )
    			return true;
    	}
    	return false;
    }

    Maybe I wasn’t doing it right so if this alternative is better than my current code, I welcome a working code that I could put straight into the widget-logic box.

    Update on my code: (1 Edit + 1 Fix)

    $category = get_the_category(); $parent = get_cat_id($category[0]->category_parent); return (is_single() && ('5'==$parent || in_category('5')));

    The 1 Edit is “get_cat_id” to replace “Manga” with its category id “5”

    The 1 Fix added the ” || in_category(‘5’) ” condition because without it, anything posted IN the top (parent) category returns false when some people might want it to be true. This is because the top (parent) category doesn’t have a parent category of itself so the $parent=”” instead of “5”.

    In my case the top (parent) category is always empty and all my posts are in sub categories so I don’t really need the “Fix” but for the sake of others, there is it.

    Thread Starter musicmasteria

    (@musicmasteria)

    Notes: I got the first part of the code from the first half of the code on this page: https://corpocrat.com/2008/10/31/how-to-get-parent-category-name-in-wordpress/

    1. <?php
    2. $category = get_the_category();
    3. $parent = get_cat_name($category[0]->category_parent);
    4. if (!empty($parent)) {
    5. echo ‘» ‘ . $parent;
    6. } else {
    7. echo ‘» ‘ . $category[0]->cat_name;
    8. }
    9. ?>

    There was an extra empty in the code that has been removed.

    My current code above does have a glitch in it though which is addressed in the code above using an if statement. My idea is that rather than echo’ing the values, save them in a third variable like $topcat and then replace the final $parent in my code above with $topcat.

    I tried this on my own and got this code:

    $category = get_the_category();
    $parent = get_cat_name($category[0]->category_parent);
    if (!empty($parent)) {
    $topcat = $parent;
    } else {
    $topcat = $category[0]->cat_name;
    }
    return (is_single() && ('Manga'==$topcat);

    But that code gives an error and I can’t find the problem in it…

    Any ideas?

    Thread Starter musicmasteria

    (@musicmasteria)

    Finally a breakthrough!

    Through a little testing I found that that this code works:
    $category = get_the_category(); $parent = get_cat_name($category[0]->category_parent); return (is_single() && ('Manga'==$parent));

    ‘Manga’ just applies to my setting but you could use this code:
    <?php $category = get_the_category(); $parent = get_cat_name($category[0]->category_parent); echo '&raquo; ' . $parent; ?>
    placed in the post you are trying to find the parent code of.

    This will output something like >>(Parent Category)
    replace your (Parent Category) with my “Manga” and there you go.

    I hear this code has limitations and works best of 1 level deep categories (perfect for me but idk about others).

    As far as i know, this code is working but I’ll be putting it through a few tests on my site before I mark this as resolved.

    Thread Starter musicmasteria

    (@musicmasteria)

    Update:

    $parentCatList = get_category_parents($cat,false,',');
    $parentCatListArray = split(",",$parentCatList);
    $topParentName = $parentCatListArray[0];
    $sdacReplace = array(" " => "-", "(" => "", ")" => "");
    $topParent = strtolower(strtr($topParentName,$sdacReplace));
    return (is_single() && ($topParent=="manga"));

    Tried the above code in widget-logic,
    I get this error:
    Catchable fatal error: Object of class WP_Error could not be converted to string in /home/animepla/public_html/testing/wp-content/plugins/widget-logic/widget_logic.php(131) : eval()'d code on line 1

    Another failed attempt… ??

    I’m trying to do something like this as well and haven’t found an answer yet…

    Mine is a little different though, it is:

    is_single() && (in_category(1) || in_category(2) || …)

    category 1,2,3… all being under the parent category of ’10’ or whatever (just making up numbers).

    Thread Starter musicmasteria

    (@musicmasteria)

    Thx. CQS plugin works well!

Viewing 8 replies - 16 through 23 (of 23 total)