• Hi !

    I’ve the following code in attempt to print the second top category and the category of a search result:

    $category = get_the_terms( $post_id , 'manualknowledgebasecat');
    $number =  $category[0] -> term_id;
    echo get_category_parents( $number, true, ' » ' );

    The previous code print the complete list of categories, but I only want it to print the second category and the current category. For example: If a search result has this categories: Products >> NameOfTheProduct >> Manual >> Configurations
    I just want to print the NameOfTheProduct and Configurations. How can I get to do that ?

    Thank you por you replies in advance.

    Best regards.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The approach depends on what level you are starting at in the hierarchy when you use get_the_terms(). If it’s always the bottom and you always want the term 2 levels up, first get the immediate parent term.
    $parent = get_term( $category[0]->parent, 'manualknowledgebasecat');

    Then get that term’s parent
    $grandparent = get_term( $parent->parent, 'manualknowledgebasecat');

    Which gives you enough data to output names or whatever
    echo "Product: {$grandparent->name} configured as {$category[0]->name}";

Viewing 1 replies (of 1 total)

The topic ‘PHP Print selected categories of the Array’ is closed to new replies.