Php Wordpress显示带有类别的自定义元数据库

Php Wordpress显示带有类别的自定义元数据库,php,html,wordpress,Php,Html,Wordpress,这是我的代码: function pages_inner_custom_box2( $post ) { // Use nonce for verification wp_nonce_field( plugin_basename( __FILE__ ), 'pages_noncename' ); // The actual fields for data entry $cats = get_post_meta( $post->ID, 'pages_cate

这是我的代码:

function pages_inner_custom_box2( $post ) {

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'pages_noncename' );

    // The actual fields for data entry
    $cats = get_post_meta( $post->ID, 'pages_categories_field', true);
    echo '<ul>';
    foreach( (get_categories() ) as $category):
        if( $category->cat_ID != 14 ):
            foreach( $cats as $values ) {
                if( $values ==  $category->cat_ID ){
                    $checked = "checked=checked";
                } else {
                    $checked = false;
                }
            }
            echo'<li id="cat-'.$category->cat_ID.'">
            <input type="checkbox" name="pages_categories_field[]" id="'
            .$category->cat_ID.'" value="'
            .$category->cat_ID.'" '
            .$checked.'> <label for="'
            .$category->cat_ID.'">'
            .__($category->cat_name, 'pages_textdomain' )
            .'</label></li>';
        endif;
    endforeach;
    echo '</ul>';
}
功能页\内部\自定义\框2($post){
//使用nonce进行验证
wp_nonce_字段(plugin_basename(uuu FILE_uuu),“pages_noncename”);
//用于数据输入的实际字段
$cats=get\u post\u meta($post->ID,'pages\u categories\u field',true);
回声“
    ”; foreach((get_categories())作为$category): 如果($category->cat_ID!=14): foreach($cats作为$value){ 如果($values==$category->cat_ID){ $checked=“checked=checked”; }否则{ $checked=false; } } echo“
  • cat_name,'pages\u textdomain') “
  • ”; endif; endforeach; 回声“
”; }
因此,它在元数据库中显示类别,现在的问题是在我更新选择后,我选择了多个类别,它将只
标记为选中的
仅一个类别。数据保存在数据库中。问题只存在于选择上


那么我做错了什么呢?

您的代码还行,但有一些小错误,因此:

function pages_inner_custom_box2( $post ) {

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'pages_noncename' );

    // The actual fields for data entry
    $cats = get_post_meta($post->ID,'pages_categories_field',true);
    echo '<ul>';
    foreach((get_categories()) as $category):
        $cat_id = $category->cat_ID;
        if($cat_id != 14):
            $checked = (in_array($cat_id,(array)$cats)? ' checked="checked"': "");
            echo'<li id="cat-'.$cat_id.'"><input type="checkbox" name="pages_categories_field[]" id="'.$cat_id.'" value="'.$cat_id.'"'.$checked.'> <label for="'.$cat_id.'">'.__($category->cat_name, 'pages_textdomain' ).'</label></li>';
        endif;
    endforeach;
    echo '</ul>';
}

我删除了我的答案,班特网更完整了作为注释复制:如果您正确地格式化和缩进了代码,第一眼就可以看出问题所在。您正在
foreach
外部执行
echo
。一个好的IDE在编程时非常有用:。
update_post_meta($post_id,'pages_categories_field',$_POST['pages_categories_field']);