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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 QAxWidget上显示一个word文档后,如何直接在该QAxWidget上修改和保存word文档
  • ¥15 Simulink仿真报错,请问如何解决
  • ¥20 宝塔面板无法添加Node项目,一直处于正在添加脚本页面
  • ¥50 Dkeil5 CT107D单片机的程序编写
  • ¥30 Ubuntu20.04中PVN3D复现过程交叉编译问题
  • ¥15 模拟电路求复阻抗和传递函数,请各位拍照写一下解答过程
  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题