WordPress 类别搜索简码不显示选项

WordPress category search shortcode not displaying options

我正在使用 wp_dropdown_categories 创建一个显示搜索框的简码,您可以在其中搜索简码中给定的类别,例如 [category-search include="1,2,3" selected="1"]。这将搜索类别 ID 的 1、2、3,并默认选择类别 ID 1。

函数如下:

function category_search($atts) {

$atts = shortcode_atts( array(
    'include' => '',    
    'selected' => '',
), $atts );
$site_url = site_url();      
$args = '"include=array( ' . $atts['include'] . ')&selected=' . $atts['selected'] . '"';                  

ob_start(); ?>

<form role="search" method="get" id="searchform" action="<?php echo $site_url ?>">
    <label class="screen-reader-text" for="s">Search for:</label>
    <input type="text" placeholder="Search" value="" name="s" id="s" />
    <?php wp_dropdown_categories( $args ); ?>
    <button type="submit" class="button"><span>Search</span></button>
</form>

<?php return ob_get_clean();
}
add_shortcode( 'category-search', 'category_search' );

它的工作原理是它显示一个搜索框,其中包含所有类别的下拉列表,但似乎没有在 $args 上选择特定类别或默认选择。我错过了什么吗?

您将 $args 设置为一个字符串,而它应该是一个数组。

尝试:$args = array('include' => $atts['include'], 'selected' => $atts['selected'])