Forums

[resolved] "elseif" not working in listing subcategories (9 posts)

  1. Anydog
    Member
    Posted 1 month ago #

    Hi.
    Please help !
    I have a problem regarding some IFELSE condition with subacategories. I use post_is_in_descentant_category function:

    <?php
    if ( $kat = 3 || post_is_in_descendant_category( 3 )  ) {
    	echo ('this is category 3 or it's descendant');
    }
    elseif ( $kat = 4|| post_is_in_descendant_category( 4 ) ) {
    	echo ('this is category 4 or it's descendant');
    }
    ?>

    When I'm in category 3 it works, but when it comes to "elseif" with category 4 it fails. Categories and subcategories are proprely set, there is no error there.
    I am obviosly missing something obviouis ... ;-)

    Thanks for help, you anonimous stranger !
    Alen

  2. apljdi
    Member
    Posted 1 month ago #

    $kat == 3 -- two equal signs. With one equal sign you are assigning a value to the variable, so in the first line you are setting $kat to 3. With two equal signs you are checking or comparing values, which is what you want.

  3. Anydog
    Member
    Posted 1 month ago #

    Hi,
    thanks for your help but still nothing ... :-(

    <?php
    if ( $cat == 3 || post_is_in_descendant_category( 3 )  ) {
    	echo ('this is category 3 or it's descendant');
    }
    elseif ( $cat == 4 || post_is_in_descendant_category( 4 ) ) {
    	echo ('this is category 4 or it's descendant');
    }
    ?>
    -- don't work
    I even tried it with in_category( 'category_name' ), still nothing ...
    Could it be that this doesn't work with multiple conditions ?

    Thanks,
    Alen

  4. Anydog
    Member
    Posted 1 month ago #

    Hah resolved it !
    With a little help of great programmers mind and Google...
    I found it here - http://www.sandboxdev.com/blog/518/get-top-parent-category-wordpress/ and implemented it this way (if anyone can benefit from it ... ):

    <?php
    $parentCatList = get_category_parents($cat,false,',');
    $parentCatListArray = split(",",$parentCatList);
    $topParentName = $parentCatListArray[0];
    $sdacReplace = array(" " => "-", "(" => "", ")" => "");
    $topParent = strtolower(strtr($topParentName,$sdacReplace));
    echo $topParent;
    if ( $topParent == 'parent_category_name1') {
    echo 'this is child of parent named '.$topParent;
    }
    else if ($topParent == 'parent_category_name1') {
    echo 'this is child of parent named '.$topParent;
    }
    ?>

    That's it. Off course, replace 'parent_category_name1' and 'parent_category_name2' with your categories and echo ... with whatever you want to do for selected (parent) category.

    Bye and good luck,
    Alen

  5. apljdi
    Member
    Posted 1 month ago #

    Did you also include the post_is_in_descendant_category() function in your code? It isn't a core function.

  6. Anydog
    Member
    Posted 1 month ago #

    No, I discarded that function, because this works perfectly. There are some other solutions, here on Codex, with post_is_in_descendant_category, but none of them worked for me ... Yeah, I supposed that function post_is_in_descendant_category is not a core function, but it exsisit, probably somewhere in wp-includes folder.
    By the way, there is a small typo in the code above - complete correct code is:

    <?php
    $parentCatList = get_category_parents($cat,false,',');
    $parentCatListArray = split(",",$parentCatList);
    $topParentName = $parentCatListArray[0];
    $sdacReplace = array(" " => "-", "(" => "", ")" => "");
    $topParent = strtolower(strtr($topParentName,$sdacReplace));
    echo $topParent;
    if ( $topParent == 'parent_category_name1') {
    echo 'this is child of parent named '.$topParent;
    }
    else if ($topParent == 'parent_category_name2') {
    echo 'this is child of parent named '.$topParent;
    }
    ?>

    Replace 'parent_category_name1' and 'parent_category_name2' with your categories.
    That's it.
    Good luck coding and support Wordpress
    =====================================================
    SHARE THE CODE
    =====================================================

  7. apljdi
    Member
    Posted 1 month ago #

    ... but it exsisit, probably somewhere in wp-includes folder.

    No it doesn't. Grep your directory. Unless you include the function somewhere it does not exist.

  8. Anydog
    Member
    Posted 1 month ago #

    Oh.
    So it doesn't.
    OK, then I should have include the function in the functions.php or somewhere, probably ... ?
    Yeah. Here http://codex.wordpress.org/Function_Reference/in_category it says I shoud do that ...

    Hmmm ... Too bad my code up works, I don't really feel like testing post_is_in ... function, 'cause I'm affraid I'm gonna mess something up .... ;)
    Well, perhaps I will ...

  9. apljdi
    Member
    Posted 1 month ago #

    If what you've got works for your purposes, all you have to gain by going back to the other function is a little bit of execution speed-- maybe. Unless you obsess about thousandths of a second, it probably isn't worth it.

Reply

You must log in to post.

About this Topic