WordPress下拉列表仅显示同级类别

时间:2018-10-10 21:11:40

标签: wordpress categories siblings

我有一个下拉列表,显示所有博客类别及其子类别。现在,我的同事认为最好只进行筛选以显示同级类别。不是父母类别或其他父母的孩子类别。仅同级类别。

我挖了一下,什么都没发现,所以我打开了这个线程。

我已经尝试过的东西:沃克班。这听起来很有希望,但是由于WP文档实际上没有任何内容,因此很难知道这是否可行。我倾向于但不倾向于,这很难说。

 <?php wp_dropdown_categories( 'show_option_none=Select a Category&hierarchical=1' ); ?>
 <script type="text/javascript">
 <!--
 var dropdown = document.getElementById("cat");
 function onCatChange() {
 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
 location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
 }
 }
 dropdown.onchange = onCatChange;
 -->
 </script>

3 个答案:

答案 0 :(得分:0)

将您的wp_dropdown_categories()调用更改为仅包含属于当前页面父类别的子页面的页面,并调整您的depth参数以不显示任何兄弟姐妹的子页面。

<?php wp_dropdown_categories( array(
    'show_option_none' => "Select a Category",
    'hierarchical' => 1,
    'child_of' => get_the_category()[0]->category_parent, // get the idea of the parent category for the current page/post
    'depth' => 1 // limit the depth of the heirarchy to only the current level
)); ?>

function reference page for wp_dropdown_categories()上有详细的记录。

答案 1 :(得分:0)

<?php wp_dropdown_categories( array(
    'show_option_none' => "Select a Category",
    'hierarchical' => 1,
    'child_of' => get_the_category()[0]->term_id,
    'depth' => 1 
)); ?>

答案 2 :(得分:0)

这非常有用。如果您有多个类别列表,并且只想显示父类别的子类别,则必须使用它。

<?php wp_dropdown_categories( array(
    'show_option_none' => "Select a Category",
    'hierarchical' => 1,
    'child_of' => get_the_category()[0]->category_parent, // get the idea of the parent category for the current page/post
    'depth' => 1 // limit the depth of the heirarchy to only the current level
)); ?>