获取自定义分类的直接子级

时间:2015-03-01 作者:Guit4eva

我已经在google上搜索了很多遍,并尽了最大努力,但我就是无法在下面的代码中找到自定义分类法的第一级子级!它仅显示父级。我正在努力实现以下目标:

Level 1  <--- at the moment, it only displays this level
  Level 2  <-- I want to display this level too
    Level 3  <-- but NOT this level
这是我目前的代码:

function my_dropdown_categories( $taxonomy, $current_selected = \'\', $include = null ) {
// Get all terms of the chosen taxonomy
$terms = get_terms($taxonomy, array(\'orderby\' => \'name\'));

// our content variable
$list_of_terms = \'<select id="location" class="selectboxSingle" name="location">\';

if ( ! is_wp_error( $terms ) ) foreach($terms as $term){

// If include array set, exclude unless in array.
if ( is_array( $include ) && ! in_array( $term->slug, $include ) ) continue;

$select = ($current_selected == $term->slug) ? "selected" : ""; // Note: ==

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

    // get children of current parent.
    // $tchildren = get_term_children($term->term_id, $taxonomy); <- gets ALL children

    $uchildren =get_terms( $taxonomy, array(\'hide_empty\' => 0, \'parent\' => $term->term_id ));

    $children = array();
    foreach ($uchildren as $child) {
        $cterm = get_term_by( \'id\', $child, $taxonomy );
        // If include array set, exclude unless in array.
        if ( is_array( $include ) && ! in_array( $cterm->slug, $include ) ) continue;
        $children[$cterm->name] = $cterm;
    }
    ksort($children);

    // PARENT TERM      
    if ($term->count > 0) {
      $list_of_terms .= \'<option class ="group-result" value="\'.$term->slug.\'" \'.$select.\'>\' . $term->name .\' </option>\';
    } else {
      $list_of_terms .= \'<option value="\'.$term->slug.\'" \'.$select.\'>\'. $term->name .\' </option>\';
    };

    // now the CHILDREN.
    foreach($children as $child) {
       $select = ($current_selected == $child->slug) ? "selected" : ""; // Note: child, not cterm
       $list_of_terms .= \'<option class="result-sub" value="\'.$child->slug.\'" \'.$select.\'>\'. $child->name.\' </option>\';
    } //end foreach
  }
}

$list_of_terms .= \'</select>\';

return $list_of_terms;
}
如果有人能帮我对付这只野兽,你就是我的超级英雄!

EDIT (更多信息基于Pieter的回答):

这是正在输出的内容(所有父级):

父级1

--父级1

--父级2

--父级3

--父级4

父级2

--父级2

--父级1

--父级3

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

这里有两个问题,一个是性能问题,另一个是大问题,如果启用调试,将返回一堆错误

这里的性能问题是您第一次获得条款。看来你在这里的目标是只获得顶级条款,而你却获得了所有条款。我想补充一下\'parent\' => 0 只获取顶级术语的参数。然后,您可以在检查的地方删除条件if ( $term->parent == 0 ) 因为所有条款都是顶级条款0 作为术语父级,因此此条件将始终返回true

你的大问题是这个代码,我不理解你的逻辑

    $uchildren =get_terms( $taxonomy, array(\'hide_empty\' => 0, \'parent\' => $term->term_id ));

    $children = array();
    foreach ($uchildren as $child) {
        $cterm = get_term_by( \'id\', $child, $taxonomy );
        // If include array set, exclude unless in array.
        if ( is_array( $include ) && ! in_array( $cterm->slug, $include ) ) continue;
        $children[$cterm->name] = $cterm;
    }
    ksort($children);
您的直系子女是正确的,因此您的$uchildren 很好。从那以后一切都乱了套

为什么要使用get_term_by() 在这里$child 已保存您使用的术语属性get_terms() 检索术语。

你不仅仅是在使用get_term_by() 这里是错误的,但您的参数是无效的,并且您正在向它传递一个完整的对象。您应该咨询法典以正确使用此功能。总之,应该从代码中删除该部分

那部分应该是这样的

    $uchildren =get_terms( $taxonomy, array(\'hide_empty\' => 0, \'parent\' => $term->term_id ));

    $children = array();
    foreach ($uchildren as $child) {
        if ( is_array( $include ) && ! in_array( $child->slug, $include ) ) continue;
        $children[$child->name] = $child;
    }
    ksort($children);
编辑这里是一个完整的工作代码,修复了几个bug。请查看我在代码中的评论以获取更新

function my_dropdown_categories( $taxonomy, $current_selected = \'\', $include = null ) 
{
    /*
     * Declare your variable first. Without this, your code has a bug if no terms are found
     */
    $list_of_terms = \'\';

    /**
    * Get all parent terms. Note we use \'parent\' => 0 to only get top level terms
    *
    * @see get_terms
    * @link http://codex.wordpress.org/Function_Reference/get_terms
    */
    $terms = get_terms( $taxonomy, array( \'orderby\' => \'name\', \'parent\' => 0 ) );

    /*
    * Use curlies here to enclose your statement. Also, check whether or not you have terms
    */
    if ( $terms && ! is_wp_error( $terms ) ) {

        /*
        * Moved this section inside your if statement. We don\'t want to display anything on empty terms
        */
        $list_of_terms .= \'<select id="location" class="selectboxSingle" name="location">\';

        foreach ( $terms as $term ) {

            // If include array set, exclude unless in array.
            if ( is_array( $include ) && ! in_array( $term->slug, $include ) ) continue;

            $select = ($current_selected == $term->slug) ? "selected" : ""; // Note: ==

            /*
             * Use the parent term term id as parent to get direct children of the term
             * Use child_of if you need to get all descendants of a term
             */
            $uchildren = get_terms( $taxonomy, array(\'hide_empty\' => 0, \'parent\' => $term->term_id ));

            $children = array();
            foreach ($uchildren as $child) {
                // If include array set, exclude unless in array.
                if ( is_array( $include ) && ! in_array( $child->slug, $include ) ) continue;
                $children[$child->name] = $child;
            }
            ksort($children);

            // PARENT TERM      
            if ($term->count > 0) {
                $list_of_terms .= \'<option class ="group-result" value="\'.$term->slug.\'" \'.$select.\'>\' . $term->name .\' </option>\';
            } else {
                $list_of_terms .= \'<option value="\'.$term->slug.\'" \'.$select.\'>\'. $term->name .\' </option>\';
            };

            // now the CHILDREN.
            foreach($children as $child) {
                $select = ($current_selected == $child->slug) ? "selected" : ""; // Note: child, not cterm
                $list_of_terms .= \'<option class="result-sub" value="\'.$child->slug.\'" \'.$select.\'>\'. $child->name.\' </option>\';
            } //end foreach

        }

        /*
        * Moved this section inside your if statement. We don\'t want to display anything on empty terms
        */
        $list_of_terms .= \'</select>\';

    }
    return $list_of_terms;
}

SO网友:Saurabh Shukla

检查此代码

$uchildren =get_terms( \'category\', array(\'hide_empty\' => false, \'parent\' => $term->term_id ));

$children = array();
foreach ($uchildren as $child) {
    $cterm = get_term_by( \'id\', $child, $taxonomy );
    // If include array set, exclude unless in array.
    if ( is_array( $include ) && ! in_array( $cterm->slug, $include ) ) continue;
    $children[$cterm->name] = $cterm;
}
此行

$cterm = get_term_by( \'id\', $child, $taxonomy ); 
假设$child是一个id(和一个整数),而:

foreach ($uchildren as $child) {

gives us an object

这也是为什么它不能按您所希望的那样工作的原因:

if ( is_array( $include ) && ! in_array( $cterm->slug, $include ) ) continue;

From here is junk that I had posted but good sense prevailed, till...

让我们做有趣的部分:

var_dump($term):
请注意每个属性的类型:

object(stdClass)(9) {
    ...
    ["parent"]=>
    string(1) "0"
    ...
}
请参见parent. 这是一根绳子。我知道,逻辑上它应该是一个整数,但它是一个字符串。很糟糕,对吧!

那么,你的if ($term->parent == 0 ) { 条件未得到满足。请尝试以下操作:

if ( empty( $term->parent ) ) {

here

结束

相关推荐

如何使用另一个文件而不是home.php

我有一个优雅的地产主题,我正在使用多个类别选择来显示一些帖子,结果页面在顶部显示功能幻灯片,我想消除这种情况;我希望它能像Wordpress Search显示结果一样工作(不带功能滑块)。我发现多类别插件正在使用get_bloginfo(\'url\'), 主页,以显示结果。所以我想创建另一个home.php, 但没有滑块;homesearch.php, 但是我如何才能调用此页而不是home.php This 是页面