Forums

Exclude categories from php the_category if their parent is... (8 posts)

  1. Eject2
    Member
    Posted 10 months ago #

    Hi,

    There's this line of code in a theme I bought:

    <div class="category"><?php the_category(', ') ?></div>

    It shows all of the categories a post is related to at the homepage and some other pages.

    There are two top categories (tag_ID=36 and tag_ID=15). A post never belongs to a top category but to multiple sub-categories that are under these 2 main categories.

    I'd like php the_category to show only sub-categories which their parent is not 36 (exclude).
    Or alternatively, show only sub-categories their parent is 15.

    How can I do it ?

    Thanks !

  2. alchymyth
    The Sweeper
    Posted 10 months ago #

    write your own code based on get_the_category() http://codex.wordpress.org/Function_Reference/get_the_category, and use conditional tags to exclude categories from the foreach loop needed to create the output.

  3. Eject2
    Member
    Posted 10 months ago #

    If only I knew how.. :)

    Unfortunately I'm not a coder. That's why I'm asking...

  4. alchymyth
    The Sweeper
    Posted 10 months ago #

    try to add this code to functions.php:

    http://pastebin.com/ZfMAuTTU

    no need to edit any other files.

    just be aware that it is quite specialized and not flexible - if the only category of the post is from a child of cat 36, it will show empty.

    make a backup copy of functions.php before editing in case you want to undo the edit.

  5. Eject2
    Member
    Posted 10 months ago #

    Thanks for the help!

    I get this error:

    Fatal error: Call to undefined function add_filter() in /home/getwebho/public_html/wp-includes/functions.php on line 4555

  6. alchymyth
    The Sweeper
    Posted 10 months ago #

    you edited the wrong functions.php file - you need to add the code to functions.php of your theme

    located somewhere here:

    /wp-content/themes/yourtheme/functions.php

  7. Eject2
    Member
    Posted 10 months ago #

    Great, it's working.

    Thank you very very much !

  8. shoshanna
    Member
    Posted 7 months ago #

    Hi, I used the code in the pastebin below and it works perfectly on the site. In the admin, however, if you go to edit a post the category list is broken in an odd way there. It's showing up as HTML link code next to each checkbox, duplicated multiple times.

    http://pastebin.com/ZfMAuTTU

    Is there any way to block this function from affecting the admin?

    Thanks!

Reply

You must log in to post.

About this Topic