• Ash

    (@ashbryant)


    Hi,

    I have created a custom post type for a restaurant menu. In this I have created some custom taxonomies for the menus the dishes appear in.

    I have called on these in the single-dish.php file I have created (think of it as the single.php for posts). This is the code I have used to list out the other menus this dish appears in…

    <?php
    // Let's do it again for another one
    $taxo_text = "";
    $the_menu = get_the_term_list( $post->ID, 'the_menu', 'This dish can be found in: ', ', ', '' );
    if ( '' != the_menu ) {
    $taxo_text .= "$the_menu<br />\n";
    }
    ?>

    This is great, but when I have a dish that is in a sub category (or “menu”) of the main menus i.e A La Carte Menu > Starters it lists it out like this.

    “This dish can be found in: A La Carte Menu, Sunday Lunch Menu, Starters, Starters”

    How do I limit it to only show the top-level “menus”, like “A La Carte Menu” & “Sunday Lunch Menu”, and stop showing “Starters”, “Main Courses” & “Desserts”

Viewing 2 replies - 1 through 2 (of 2 total)
  • Well this is old and there is for sure a better method but since this is the 1st link on google i will post a quick solution:
    function removeChildrens($var)
    {
    return $var->parent == 0;
    }
    function getValues($arr)
    {
    $str = ”;
    foreach($arr as $item)
    {
    if(strlen($str)>0) $str.=’,’;
    $str.=$item->name;
    }
    return $str;
    }
    $terms = wp_get_object_terms($post->ID,’turism-tara’);
    $taxonomyTara = array_filter($terms,”removeChildrens”);
    echo getValues($taxonomyTara);

    Thank you so much alicornea – I’ve been trying to figure out how to do this for a few days now.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Limit the results returned by get_the_term_list’ is closed to new replies.