可锐资源网

技术资源分享平台,提供编程学习、网站建设、脚本开发教程

wordpres调用指定分类下面子分类

       <?php
                        // 获取主分类 "News" 的信息(通过slug获取,确保与您的站点结构一致)
                        $parent_category = get_term_by('slug', 'news', 'category');
                        if ($parent_category) : ?>
                            <h3 class="widget-title">
                                <span>
                                    <a href="<?php echo esc_url(get_category_link($parent_category->term_id)); ?>">
                                        <?php echo esc_html($parent_category->name); ?>
                                    </a>
                                </span>
                            </h3>
                            <ul>
                                <?php
                                // 查询主分类下的子分类(深度为1,仅显示直接子分类)
                                $child_categories = get_categories(array(
                                    'parent' => $parent_category->term_id,
                                    'depth' => 1,
                                    'orderby' => 'name',
                                    'hide_empty' => false
                                ));

                                foreach ($child_categories as $child_category) : ?>

                                    <li class="cat-item cat-item-<?php echo esc_attr($child_category->term_id); ?>">
                                        <a href="<?php echo esc_url(get_category_link($child_category->term_id)); ?>">
                                            <?php echo esc_html($child_category->name); ?>
                                        </a>
                                        <span class="plus-minus"></span> <!-- 加减号图标容器 -->
                                    </li>

                                <?php endforeach; ?>
                            </ul>

                        <?php endif; ?>
                    </section>
                    <?php
                    // 调用页脚侧边栏(支持多个小工具并列显示)
                    if ( is_active_sidebar( 'primary-abouts-sidebar' ) ) {
                        dynamic_sidebar( 'primary-abouts-sidebar');
                    }
                    ?>

实现如下样式

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言