Php 在WordPress类别下拉列表中传递选定值

Php 在WordPress类别下拉列表中传递选定值,php,jquery,wordpress,post,custom-wordpress-pages,Php,Jquery,Wordpress,Post,Custom Wordpress Pages,我有一个自定义分类法,并且在归档页面中添加了一个类别下拉列表,允许用户在类别之间快速切换。我有以下代码,用于将用户重定向到他们选择的新类别。但是,下拉列表始终默认为中的第一个选项。当用户切换类别时,如何传递所选的,并将其设置为默认值 <?php $args = array( 'taxonomy' => 'custom_taxonomy', 'orderby' => 'name', 'show_count' => 1, 'hierarc

我有一个自定义分类法,并且在归档页面中添加了一个类别下拉列表,允许用户在类别之间快速切换。我有以下代码,用于将用户重定向到他们选择的新类别。但是,下拉列表始终默认为
中的第一个选项。当用户切换类别时,如何传递所选的
,并将其设置为默认值

<?php
$args = array(
  'taxonomy'     => 'custom_taxonomy',
  'orderby'      => 'name',
  'show_count'   => 1,
  'hierarchical' => 1,
  'title_li'     => 'title'
);

<form id="categoriesform" action="<?php bloginfo('url'); ?>" method="post">
    <div>
        <?php $cats = get_categories($args); ?>
        <select id="categories" name="custom_taxonomy">
            <option value="allCategories">All Categories</option>
            <?php foreach ($cats as $cat) : ?>
                <option value="<?php echo get_term_link($cat, $cat->taxonomy) ?>">
                <?php echo $cat->name ?></option>
            <?php endforeach; ?>
        </select>
    </div>
</form>
<script>
jQuery(document).ready(function() {
    jQuery('#categories').change(function() {
        if (jQuery(this).val() == 'allCategories') {
            window.location = 'http://example.com/all-categories';
        } else {
            window.location = jQuery(this).val();
        }
    });
});
</script>

首先需要通过
get\u queryed\u object()->term\u id然后选中并添加“选定项”选项

<?php
 $current=get_queried_object()->term_id;
$args = array(
 'taxonomy'     => 'custom_taxonomy',
 'orderby'      => 'name',
 'show_count'   => 1,
 'hierarchical' => 1,
 'title_li'     => 'title'
);

<form id="categoriesform" action="<?php bloginfo('url'); ?>" method="post">
<div>
    <?php $cats = get_categories($args); ?>
    <select id="categories" name="custom_taxonomy">
        <option value="allCategories">All Categories</option>
        <?php foreach ($cats as $cat) : ?>
            <option <?php echo ($current == $cat->term_id ? 'selected' : ''); ?> value="<?php echo get_term_link($cat, $cat->taxonomy) ?>">
            <?php echo $cat->name ?></option>
        <?php endforeach; ?>
    </select>
</div>
</form>
<script>
jQuery(document).ready(function() {
   jQuery('#categories').change(function() {
    if (jQuery(this).val() == 'allCategories') {
        window.location = 'http://example.com/all-categories';
    } else {
        window.location = jQuery(this).val();
    }
});
});
</script>