第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果選中了父類別,則不要回顯子類別

如果選中了父類別,則不要回顯子類別

PHP
慕尼黑5688855 2022-09-12 13:01:48
我有這個大陸 -> 國家/地區(qū) 類別設(shè)置自定義帖子類型。- Africa (parent 1)    - Uganda    - Zambia    - Zimbabwe- Asia (parent 2)    - Afghanistan    - Bahrain    - Bangladesh    - Bhutan如果為帖子選中了父類別,請不要重復(fù)子類別。(即使檢查了一個或多個孩子)echo => Africa, Asia如果選中了一個或多個子類別,但未選中父類別,則進(jìn)行還原。僅顯示子類別。echo => Uganda, Zambia, Zimbabwe, Afghanistan, Bahrain, Bangladesh, Bhutan更新此外,如果選中非洲(父 1),而檢查亞洲(父 2)不檢查,但檢查阿富汗和不丹(父 2 的子項(xiàng)),則輸出應(yīng)為:echo => Africa, Afghanistan, Bhutan.僅當(dāng)選中了一個或多個父類別時,才會輸出。<?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貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個贊

請嘗試以下代碼,它可以幫助您了解邏輯,您可以根據(jù)需要對其進(jìn)行修改以滿足您的輸出要求


$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;


查看完整回答
反對 回復(fù) 2022-09-12
?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個贊

我設(shè)法找出了這個問題的解決方案!這是經(jīng)過測試并產(chǎn)生我想要的結(jié)果!如果您有更優(yōu)雅的解決方案,請告訴我!


    <?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);


    ?>


查看完整回答
反對 回復(fù) 2022-09-12
  • 2 回答
  • 0 關(guān)注
  • 85 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號