为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕尼黑5688855 2022-09-12 13:01:48
我有这个大陆 -> 国家/地区 类别设置自定义帖子类型。- 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;    ?>如果未选中子类别的父类别,如何输出子类别?
查看完整描述

2 回答

?
收到一只叮咚

TA贡献1821条经验 获得超5个赞

请尝试以下代码,它可以帮助您了解逻辑,您可以根据需要对其进行修改以满足您的输出要求


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

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

$outputparent = $outputchild = array();

foreach( $terms as $term ) :

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

        $outputparent[] = '<a href="' . esc_url( get_term_link( $term ) ) . 

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

            '</a> ';            

    else :

        $outputchild[] = '<a href="' . esc_url( get_term_link( $term ) ) . 

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

            '</a>';

    endif; //Endif

endforeach;


if( !empty( $outputparent ) ) :

    echo 'Parent category is checked<br>';

    echo implode('<br>', $outputparent);

    $outputchild = array();

elseif( !empty( $outputchild ) && empty( $outputparent ) ) :

    echo 'Only Childs<br>';

    echo implode('<br>', $outputchild); 

endif;


查看完整回答
反对 回复 2022-09-12
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

我设法找出了这个问题的解决方案!这是经过测试并产生我想要的结果!如果您有更优雅的解决方案,请告诉我!


    <?php


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


    // If term is a parent, add to post_parent array.

    $post_parent = array();

    foreach( $categories as $parent_id ) {

        if($parent_id->parent < 1) {

            $post_parent[] = $parent_id->term_id;

        }

    }


    // If terms parentId does not exist in post_parent array

    // add to array regions as a key => value pair

    $regions = array();

    foreach( $categories as $category ) {

        if (!in_array($category->parent, $post_parent)) {

            $regions[$category->term_id] = $category->name;

        }

    }


    // Sort terms based on keys (regions), impolde and print

    ksort($regions);

    $locations = array();

    foreach($regions as $key => $value) {

      $locations[] = ' <a href="https://www.google.com/maps?q=' . $value . '">' . $value . '</a>';

    }


    echo implode(",", $locations);


    ?>


查看完整回答
反对 回复 2022-09-12
  • 2 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号