dongxiaowei_1234 2017-03-22 21:08 采纳率: 0%
浏览 202
已采纳

Wordpress WP_Term_Query自定义分类选择选项

The below code works really well for pulling in a select option list for all the entries in a custom taxonomy called 'make'. It only pulls in parent level ones which is perfect.

            <select id="make" name="make">
                <option value="">--</option>
                <?php $term_query = new WP_Term_Query( array( 'taxonomy' => 'make', 'parent' => 0 ) ); if ( ! empty( $term_query->terms ) ) {
                    foreach ( $term_query ->terms as $term ) {
                        echo '<option class="' . $term->name . '" value="' . $term->name . '">' . $term->name . '</option>';
                    }
                } else {    
                };?>
            </select>

However, i would like to create another identical version of this code but instead of pulling in a list of all the parent values, it should pull in all the child values. Not for a particular page or post, but should output a select list of ALL the child entries of a custom taxonomy, ignoring the parents. Any help is massively appreciated!

  • 写回答

1条回答 默认 最新

  • douxigai8757 2017-03-22 23:51
    关注

    you could use get_term_children and get_term_by functions to build your second select:

    <select id="make" name="make">
    <option value="">--</option>
    <?php
         $term_query = new WP_Term_Query( array( 'taxonomy' => 'make', 'parent' => 0 ) );
         if ( ! empty( $term_query->terms ) ) {
            foreach ( $term_query ->terms as $term ) {
                $term_children = get_term_children( $term->term_id, 'make' );
                foreach($term_children as $children){
                    $child = get_term_by( 'id', $children, 'make' );
                    echo '<option class="' . $child->name . '" value="' . $child->name . '">' . $child->name . '</option>';
                }
            }
         } else {
         };
    ?>
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建