【问题标题】:Display subcategories in wordpress在 wordpress 中显示子类别
【发布时间】:2020-09-28 15:01:30
【问题描述】:

以下代码显示父类别图像 我可以在父类别图像下显示子类别吗

<?php $cat = get_query_var('cat');
$args=array('term_args'=>array('child_of'=>$cat,'hide_empty'=>0,'orderby'=>'id'));
$terms = apply_filters( 'taxonomy-images-get-terms','',$args );
if ( ! empty( $terms ) ) {
foreach ( (array) $terms as $term ) {
   echo $this_category;
   ?>
<li class="col-md-3">
 <a href="<?php echo $term->name; ?>"><?php echo wp_get_attachment_image( $term->image_id, 'detail' ) ?></a>
</li>
<?php
}
}
 ?>

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

    我制作了两个自定义函数来获取项目中的父子类别。 创建一个名为 project-helper.php 的文件并转到 functions.php,最后添加您创建的 project-helper.php 文件到 functions.php

    //functions.php
    require 'project-helper.php';
    

    // project-helper.php

    /**
     * Get Category
     * @param $taxonomy
     * @param string $order
     * @return array
     */
    function getPostTermCategory($taxonomy, $order = 'asc')
    {
        $term_query = new WP_Term_Query([
            'taxonomy' => $taxonomy,
            'order' => $order,
            'hide_empty' => false,
            'parent' => 0
        ]);
        $categories = [];
        foreach ($term_query->terms as $term) {
            $categories[] = $term;
        }
        return $categories;
    }
    
    /**
     * Get Child Categories of Parent
     * @param $taxonomy
     * @param $parentId
     * @param string $order
     * @return array
     */
    function getPostTermChildCategory($taxonomy, $parentId, $order = 'asc')
    {
        $term_query = new WP_Term_Query([
            'taxonomy' => $taxonomy,
            'order' => $order,
            'hide_empty' => false,
            'parent' => $parentId
        ]);
        $categories = [];
        foreach ($term_query->terms as $term) {
            $categories[] = $term;
        }
        return $categories;
    }
    

    现在从您的模板中调用创建的函数。

    $projectRegions = getPostTermCategory('project-region-cat');
    foreach ($projectRegions as $projectRegion) {
                        
        <?php $categoryChild = getPostTermChildCategory('project-region-cat', $projectRegion->term_id);
            foreach ($categoryChild as $categoryChild) { ?>
            <option value="<?= $categoryChild->slug; ?>"><?= $categoryChild->name; ?></option>
        <?php } ?>
    }
    

    我使用了 select html 标记的 option。你可以用你的循环做任何事情 //这里project-region-cat是父类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      相关资源
      最近更新 更多