Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何保存选择下拉式wordpress元数据库_Php_Wordpress - Fatal编程技术网

Php 如何保存选择下拉式wordpress元数据库

Php 如何保存选择下拉式wordpress元数据库,php,wordpress,Php,Wordpress,如何保存选择下拉式wordpress元数据库 在选项中,有一个来自自定义帖子类型的循环 <select name="music[artists]" class="skant-select" id="artists" style="width: 90%;" multiple="multiple"> <?php $artists_meta_box_args = array( 'post_type' => array('music_artist'), //'p

如何保存选择下拉式wordpress元数据库

在选项中,有一个来自自定义帖子类型的循环

<select name="music[artists]" class="skant-select" id="artists" style="width: 90%;" multiple="multiple">

<?php
$artists_meta_box_args = array(
    'post_type' => array('music_artist'),
    //'posts_per_page' => 20,
);
$artists_meta_box = new WP_Query($artists_meta_box_args);
if ($artists_meta_box->have_posts()):
while ($artists_meta_box->have_posts()):$artists_meta_box->the_post();
    global $post;
    $artistFirstname = get_post_meta($post->ID, 'artist_firstname', true);
    $artistLastname = get_post_meta($post->ID, 'artist_lastname', true);
?>
<option value="<?php echo $artistFirstname . '&nbsp' . $artistLastname; ?>"><?php echo $artistFirstname . '&nbsp' . $artistLastname; ?></option>
    <?php
endwhile;
endif;
?>

 </select>

我还没有测试过,但我认为它应该可以工作:

function save_custom_meta_box($post_id, $post, $update) {

    // Check if user is allowed to edit this
    if(!current_user_can("edit_post", $post_id))
        return $post_id;

    // Don't save meta on auto save
    if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
        return $post_id;

    // Check if post-type (Change $slug to post-type metabox is in)
    $slug = "post";
    if($slug != $post->post_type)
        return $post_id;

    $meta_box_dropdown_value = "";

    // If has value to save (remember to change "metabox-name" to the dropdown name).
    if(isset($_POST["artist_dropdown"])) {
        $meta_box_dropdown_value = $_POST["artist_dropdown"];
    }   
    // Save the meta under meta-key "artist"
    update_post_meta($post_id, "artist", $meta_box_dropdown_value);

}

add_action("save_post", "save_custom_meta_box", 10, 3);
我希望这些评论能为你解释这一点。只需记住替换上面提到的值

您还需要在保存值后选择哪个值的渲染下拉列表中添加一个函数。因此,metabox中渲染的下拉列表应如下所示:

<select name="artist_dropdown">
<?php 

    $args = array(
        'post_type' => array('music_artist')
    );

    $posts = get_posts($args);

    $option_values = array();

    if ( $posts ) {
        foreach ( $posts as $post ) {
            $artistFirstname = get_post_meta($post->ID, 'artist_firstname', true);
            $artistLastname = get_post_meta($post->ID, 'artist_lastname', true);
            $option_values[] = $artistFirstname . ' ' . $artistLastname;
        }
    }

    foreach($option_values as $key => $value) {
        if($value == get_post_meta($object->ID, "artist", true)) {
            ?>
                <option selected><?php echo $value; ?></option>
            <?php    
        } else {
            ?>
                <option><?php echo $value; ?></option>
            <?php
        }
    }
?>
</select>  


顺便说一句,这里有一个很好的教程,你还应该看看这是一个非常棒的用于构建元数据库的工具包。

如何显示下拉列表?你有更多的代码要显示吗?您想如何保存选项?我想在刷新后选择保存选项并在网站上使用