如何在WordPress目录主题中仅显示父级分类并修改搜索选项名
修改目录站点搜索分类下拉为仅显示父级地点分类并重命名
以下是修改后的完整代码,直接替换原代码即可实现你的需求:
<div class="col-lg-4"> <div class="dropdown dropdown-category"> <select name="dropdown_category4" class="btn btn-secondary dropdown-toggle" type="button" id="listing_category"> <option value="" selected><?php _e('Search by: Location', 'directorytheme'); ?></option> <?php // 仅获取顶级父分类(parent=0) $terms_links = get_terms( array( 'taxonomy' => 'listing-categories', 'parent' => 0, 'hide_empty' => false // 可选:设为true则隐藏没有关联内容的分类,根据你的需求调整 ) ); foreach( $terms_links as $terms_link ) : ?> <option class="dropdown-item dropdown-cat" value="<?php echo "{$terms_link->slug}"; ?>"><?php echo "{$terms_link->name}"; ?></option> <?php endforeach; ?> </select> <?php if($listing_category != '') : ?> <input type="hidden" name="s_listing_category" id="s_listing_category" value ="<?php echo $listing_category; ?>"> <?php else : ?> <input type="hidden" name="s_listing_category" id="s_listing_category"> <?php endif; ?> </div><!--dropdown--> </div>
关键修改说明:
- 重命名默认选项文本:把原代码中
_e('Search by: Category', 'directorytheme');的字符串替换为'Search by: Location',下拉菜单的默认提示文本就会变成你需要的内容。如果主题支持翻译,后续可在主题语言文件中添加对应翻译条目,直接修改字符串也能立即生效。 - 仅加载父级分类:修改
get_terms的调用逻辑,传入参数数组指定parent => 0,这会让WordPress只获取listing-categories分类法下的顶级父分类(不包含任何子分类)。hide_empty参数可按需调整:若要隐藏无关联目录项的地点分类,设为true;若要显示所有父分类(无论是否有内容),设为false即可。
内容的提问来源于stack exchange,提问作者Jaime
相关产品推荐
相关产品推荐

