如果选中了父类别,则不要回显子类别

我有这个大陆 -> 国家/地区 类别设置自定义帖子类型。


- Africa (parent 1)

    - Uganda

    - Zambia

    - Zimbabwe

- Asia (parent 2)

    - Afghanistan

    - Bahrain

    - Bangladesh

    - Bhutan

如果为帖子选中了父类别,请不要重复子类别。(即使检查了一个或多个孩子)echo => Africa, Asia


如果选中了一个或多个子类别,但未选中父类别,则进行还原。仅显示子类别。echo => Uganda, Zambia, Zimbabwe, Afghanistan, Bahrain, Bangladesh, Bhutan


更新此外,如果选中非洲(父 1),而检查亚洲(父 2)不检查,但检查阿富汗和不丹(父 2 的子项),则输出应为:echo => Africa, Afghanistan, Bhutan.


仅当选中了一个或多个父类别时,才会输出。


<?php


$post = get_post(); // If $post is already available, skip.

$terms = get_the_terms( $post->ID, 'custom-category' );

foreach ( $terms as $term ) :

    if ( $term->parent === 0 ) :

        echo '<a href="' . esc_url( get_term_link( $term->term_id, 'custom-category' ) ) . 

            '" title="' . esc_html( $term->name ) . '" ' . '>' . esc_html( $term->name ) . 

            '</a> ';

    endif;

endforeach;


    ?>

如果未选中子类别的父类别,如何输出子类别?


慕尼黑5688855
浏览 87回答 2
2回答

收到一只叮咚

请尝试以下代码,它可以帮助您了解逻辑,您可以根据需要对其进行修改以满足您的输出要求$post = get_post(); // If $post is already available, skip.$terms = get_the_terms( $post->ID, 'category' );&nbsp; &nbsp;&nbsp;$outputparent = $outputchild = array();foreach( $terms as $term ) :&nbsp; &nbsp; if( $term->parent === 0 ) :&nbsp; &nbsp; &nbsp; &nbsp; $outputparent[] = '<a href="' . esc_url( get_term_link( $term ) ) .&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '" title="' . esc_html( $term->name ) . '" ' . '>' . esc_html( $term->name ) .&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</a> ';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; else :&nbsp; &nbsp; &nbsp; &nbsp; $outputchild[] = '<a href="' . esc_url( get_term_link( $term ) ) .&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '" title="' . esc_html( $term->name ) . '" ' . '>' . esc_html( $term->name ) .&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</a>';&nbsp; &nbsp; endif; //Endifendforeach;if( !empty( $outputparent ) ) :&nbsp; &nbsp; echo 'Parent category is checked<br>';&nbsp; &nbsp; echo implode('<br>', $outputparent);&nbsp; &nbsp; $outputchild = array();elseif( !empty( $outputchild ) && empty( $outputparent ) ) :&nbsp; &nbsp; echo 'Only Childs<br>';&nbsp; &nbsp; echo implode('<br>', $outputchild);&nbsp;endif;

繁华开满天机

我设法找出了这个问题的解决方案!这是经过测试并产生我想要的结果!如果您有更优雅的解决方案,请告诉我!&nbsp; &nbsp; <?php&nbsp; &nbsp; $categories = get_the_terms( $post->ID, 'custom-category' );&nbsp; &nbsp; // If term is a parent, add to post_parent array.&nbsp; &nbsp; $post_parent = array();&nbsp; &nbsp; foreach( $categories as $parent_id ) {&nbsp; &nbsp; &nbsp; &nbsp; if($parent_id->parent < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post_parent[] = $parent_id->term_id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // If terms parentId does not exist in post_parent array&nbsp; &nbsp; // add to array regions as a key => value pair&nbsp; &nbsp; $regions = array();&nbsp; &nbsp; foreach( $categories as $category ) {&nbsp; &nbsp; &nbsp; &nbsp; if (!in_array($category->parent, $post_parent)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $regions[$category->term_id] = $category->name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Sort terms based on keys (regions), impolde and print&nbsp; &nbsp; ksort($regions);&nbsp; &nbsp; $locations = array();&nbsp; &nbsp; foreach($regions as $key => $value) {&nbsp; &nbsp; &nbsp; $locations[] = ' <a href="https://www.google.com/maps?q=' . $value . '">' . $value . '</a>';&nbsp; &nbsp; }&nbsp; &nbsp; echo implode(",", $locations);&nbsp; &nbsp; ?>
打开App,查看更多内容
随时随地看视频慕课网APP