WP jump menu to display sub-categories

时间:2018-03-23 00:48:36

标签: wordpress

I am using the following bit of code to create a jump-menu to display a list of posts in the category 'Consultant' which displays a list of post titles. Can someone help modify this code so that instead of a list of post titles it shows a list of sub-categories? (The sub-categories are locations).

<!-- sidebar drop-down list -->
<div class="consultant-sidebar">
  <h3>Find consultant by name</h3>
  <?php
$cat_id = get_cat_ID('consultant');
$args=array(
'cat' => $cat_id,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
?>
  <form name="jump">
    <select name="menu">
      <?php
while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <option value="<?php the_permalink() ?>">
      <?php the_title(); ?>
      </option>
      <?php
endwhile;
}
?>
    </select>
    <input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="Go">
  </form>
  <?php
wp_reset_query();
?>
  <h3><a href="https://gouldingprocess.com/interns/">Sleeptalk® Interns</a></h3>
</div>
<!--  --> 

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码替换代码并进行检查吗?

<!-- sidebar drop-down list -->
<div class="consultant-sidebar">
  <h3>Find consultant by name</h3>
<?php
$cat_id = get_cat_ID('consultant');
$categories = get_categories( array( 'child_of' => $cat_id ,'hide_empty' =>true) );
?>
<form name="jump">
    <select name="menu">
<?php     
foreach($categories as $category) {?>
     <option value="<?php echo get_category_link( $category->term_id );?>">
      <?php echo $category->name; ?>
      </option>
<?php }?>
</select>
 <input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="Go">
</form>
<?php
wp_reset_query();
?>
    <h3><a href="https://gouldingprocess.com/interns/">Sleeptalk® Interns</a></h3>
</div>
<!--  -->

注意:在上面的代码中,我已将“hide_empty”传递给“True”,因此它只会考虑分配了超过0个帖子的“顾问”子类别。