dqgg25493 2016-09-30 21:26
浏览 33
已采纳

获取元框以在更新后保持选择

I'm building a meta box for one of my custom post types that has a selection option. I'm trying to make it so that when you make a selection and update the post, the option stays selected when the page reloads. I've found a few people on StackOverflow working on the same and I've followed those suggestions but haven't quite figured it out yet. If anyone has any suggestions, any help is appreciated.

<?php

function property_info_meta_box() {
    add_meta_box('property-info', 'Property', 'property_info_cb', 'properties', 'normal', 'high'); 
}
add_action('add_meta_boxes', 'property_info_meta_box');

function property_info_cb($propertyInfo) { 

    $selectAgent = get_post_meta($propertyInfo->ID, 'select-agent-value', true);

    ?>

    <label for="select-agent-text">Select Agent</label> <br>
    <select multiple size="5" name="select-agent" id="select-agent">

        <option value="none">None</option>

            <?php

            $args = array(
                'post_type' => 'agents',
                'posts_per_page' => -1
                );

            $posts = new WP_Query($args);

            if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post(); ?>

                <option value="<?php the_ID() ?>" <?php selected( $selectAgent, the_ID() ); ?>> <?php the_title(); ?> </option>

            <?php endwhile; endif; ?>

    </select>

<?php }

function add_property_info_fields ($propertyInfoId, $propertyInfo){
    if ($propertyInfo->post_type == 'properties') {

        if(isset($_POST['select-agent'])){
            update_post_meta($propertyInfoId, 'select-agent-value', $_POST['select-agent']);
        }

    }
}
add_action('save_post', 'add_property_info_fields', 10, 2);
  • 写回答

1条回答 默认 最新

  • duanji4449 2016-09-30 22:08
    关注

    Shot in the dark here, but you are using the_ID() improperly. This function prints the ID to the screen. You are trying to return the ID for use as a function parameter. You should try something like:

    <?php selected( $selectedAgent, get_the_ID() ); ?>

    See get_the_ID() vs the_ID()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题