下载时,WordPress会获得子类别的子项

时间:2015-02-26 16:42:49

标签: php wordpress

我正在尝试让子类别的孩子填充下拉列表,我正在努力将foreach循环中的不同类别分开 - 目前我的代码看起来像:

<?php

                $taxonomy = 'anatomy';
                $parents  = get_terms( $taxonomy, array( 'parent' => 0, "hide_empty" => 0 ) ); // Get all top level terms of a taxonomy

                if ( $parents ) :
                ?>
                    <select name="anatomy" id="Anatomy">
                      <option value="">Anatomy</option>
                        <?php foreach ( $parents as $term ) { ?>
                             <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
                        <?php } ?>
                    </select>
                    <?php  $states = get_term_children( $term->term_id, $taxonomy ); ?>
                    <select name="state" id="state" disabled="disabled">
                      <option value="">Choose a state</option>
                        <?php foreach ( $states as $child ) { 
                                      $term = get_term_by( 'id', $child, $taxonomy ); ?>
                                  <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
                            <?php } ?>
                    </select>

                <?php endif;?>
                <select name="diagnosis" id="diagnosis" disabled="disabled">
                  <option value="">Diagnosis</option>
                 POPULATE WITH CHILD OF CHILD CATEGORIES
                </select>

任何帮助都会很棒 - 谢谢:)

1 个答案:

答案 0 :(得分:0)

如果我理解你的评论答案是正确的,那就好像是:

$taxonomy = 'anatomy';
$parents  = get_terms( $taxonomy, array( 'parent' => 0, "hide_empty" => 0 ) ); // Get all top level terms of a taxonomy

if ( $parents ) :
    ?>
    <select name="anatomy" id="Anatomy">    
        <option value="">Anatomy</option>
        <?php foreach ( $parents as $term ) { ?>
        <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
         <?php } ?>
    </select>
    <?php $firstParent = $parents[0]; ?>
    <?php $states = get_term_children( $firstParent->term_id, $taxonomy ); ?>
    <select name="state" id="state" disabled="disabled">
        <option value="">Choose a state</option>
        <?php foreach ( $states as $child ) { 
            $term = get_term_by( 'id', $child, $taxonomy ); ?>
            <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
        <?php } ?>
    </select>
    <?php $firstState = $states[0]; ?>
    <?php $diags = get_term_children( $firstState ->term_id, $taxonomy ); ?>
    <select name="diagnosis" id="diagnosis" disabled="disabled">
        <option value="">Diagnosis</option>
        <?php foreach ( $diags as $child ) { 
            $term = get_term_by( 'id', $child, $taxonomy ); ?>
            <option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
        <?php } ?>
    </select>
<?php endif;?>