Php 多选类别WordPress自定义控件

Php 多选类别WordPress自定义控件,php,wordpress,Php,Wordpress,我正在尝试创建一个多选框类别下拉列表,我有一个问题 以下是自定义控件类: class Nt_Customize_Control_Multiple_Select extends WP_Customize_Control { /** * The type of customize control being rendered. */ public $type = 'multiple-select'; /** * Displays the multiple select on the cus

我正在尝试创建一个多选框类别下拉列表,我有一个问题

以下是自定义控件类:

class Nt_Customize_Control_Multiple_Select extends WP_Customize_Control {

/**
 * The type of customize control being rendered.
 */
public $type = 'multiple-select';

/**
 * Displays the multiple select on the customize screen.
 */
public function render_content() {

if ( empty( $this->choices ) )
    return;
?>
    <label>
        <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
        <select <?php $this->link(); ?> multiple="multiple" style="height: 100%;">
            <?php
                foreach ( $this->choices as $value => $label ) {
                    $selected = ( in_array( $value, $this->value() ) ) ? selected( 1, 1, false ) : '';
                    echo '<option value="' . esc_attr( $value ) . '"' . $selected . '>' . $label . '</option>';
                }
            ?>
        </select>
    </label>
<?php }}
以及类别功能:

 function nt_cats() {
  $cats = array();
  $cats[0] = "All";
  foreach ( get_categories() as $categories => $category ) {
    $cats[$category->term_id] = $category->name;
  }
  return $cats;
}

任何帮助都将不胜感激,谢谢

您指定了一个消毒功能
nt\u sanitize\u cat
,您定义了该功能吗

$wp_customize->add_setting( 'nt_featured_cat', array(
    'default' => 0,
    'transport'   => 'refresh',
    'sanitize_callback' => 'nt_sanitize_cat' 
));
我实现了您的代码并添加了这个清理函数,我得到了一个返回值数组:

/**
 * Validate the options against the existing categories
 * @param  string[] $input
 * @return string
 */
function nt_sanitize_cat( $input )
{
    $valid = nt_cats();

    foreach ($input as $value) {
        if ( !array_key_exists( $value, $valid ) ) {
            return [];
        }
    }

    return $input;
}

你有什么问题?你有什么错误吗?@AndrewMyers我正在使用
WP\u Query
进行循环,该类别不打印任何内容,
var\u dump
返回
字符串“”(长度=0)
/**
 * Validate the options against the existing categories
 * @param  string[] $input
 * @return string
 */
function nt_sanitize_cat( $input )
{
    $valid = nt_cats();

    foreach ($input as $value) {
        if ( !array_key_exists( $value, $valid ) ) {
            return [];
        }
    }

    return $input;
}