• Robin Cannon

    (@fogofeternity)


    I’m trying to exclude certain categories in my entry-utility.

    How can I exclude specific categories when calling:

    get_the_category_list(‘, ‘)

    Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • esmi

    (@esmi)

    get_the_category_list? Do you mean get_the_category?

    There is indeed an under-documented get_the_category_list function.

    I wanted to exclude “Artwork” and “Gallery” from my listings in the manner you described and was able to do so by modifying the category-template.php file in the wp-includes directory. Unfortunately, I think this means you will have to reapply the patch with every wordpress update. :-/

    After line 157 AND line 179, which say:
    foreach ( $categories as $category ) {
    Add the following code:
    //added this line to stop these cats from appearing in link lists
    if($category->name=='Gallery'||$category->name=='Artwork') continue;

    I know you don’t have to add the comment, but it always helps to remind yourself what changes you make to the templates…

    Hope this helps,

    AKA

    Following on from what you’ve done akamediasystem,

    I’ve taken your 2nd line of code and added this within the loop of my single.php in my theme rather than hacking the core code and it seems to work

    <div class="entry-meta">
         <?php
    	foreach((get_the_category()) as $category) {
    	echo $category->cat_name ;
    	if($category->name=='text'||$category->name=='audio') continue;
          ?>
    </div>

    hope this helps

    I’ve added the links to the categories but can anyone help me with something I’ve left out?

    <?php
    	foreach((get_the_category()) as $category) {
    	echo '<a href="'.$category_link = get_category_link( $category_id ).' "> '.$category->cat_name.'</a>' ;
    	if($category->name=='text'||$category->name=='audio'||$category->name=='video') continue; }
    ?>

    is returning the following:

    <a href="http://www.pathtolink/?cat= "> 1940s</a>

    I want it to show the category id like below in the link url, hat am i missing?

    <a href="http://www.pathtolink/ROH/?cat=29 "> 1940s</a>

    I think that the quotes might be a little messed up?
    You have:

    echo '<a href="'.$category_link = get_category_link( $category_id ).' "> '.$category->cat_name.'</a>' ;

    but perhaps try:

    echo '<a href="'.$category_link'.=.'get_category_link( $category_id ).' "> '.$category->cat_name.'</a>' ;

    thanks for the replay but that’s throwing up a parsing error 🙁

    What about just trying get_category_link($category_id)

    No that’s not the problem. The syntax is fine I’m just missing a variable call somewhere so I’m going to search the forums and post elsewhere thanks

    This worked for me:

    <?php
    foreach((get_the_category()) as $category) {
    if($category->name=='xxx'||$category->name=='yyy') continue;
    $category_id = get_cat_ID( $category->cat_name );
    $category_link = get_category_link( $category_id );
    echo '<li><a href="'.$category_link.'">'.$category->cat_name.'</a></li>';
    } ?>

    Hope this helps
    ddmarker

    That worked a treat and so simple! Thanks so much for your help!! 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to exclude categories from get_the_category_list?’ is closed to new replies.