使用数据库表值自动填充元框下拉列表

问题描述

我很绝望!我正在尝试填充一个下拉自定义字段元框,其中的值来自我创建的由不同插件创建的数据库

我尝试了各种不同的方法,但这是我第一次使用自定义帖子和自定义字段,所以有点新手。

在附加的代码中,如果我用键入的值数组替换函数“grab_locations”,它就可以工作。

我的表 nsc_locations 有许多字段,其中 nl_location_id 和 nl_location_name 是我希望出现在选择下拉列表中的字段。

我使用的是 wordpress 5.6 版和 PHP 7.4.13 版

非常感谢!

<?PHP
/*
Plugin Name: NCS Events
Description: This plugin has been designed to add events on the northants County Scouts website using custom post types
Author: Henny Cameron 
Author URI: http://www.rumpleteaser.co.uk
Text Domain: ncs-events
Version: 0.35
*/

/* v0.1  23/12/2020  initial
 
*/

// Define the tables used in NCS event
global $wpdb;

define('NCS_LOCATION_TABLE',$wpdb->prefix . 'ncs_location');
define('NCS_PERSON_TABLE',$wpdb->prefix . 'ncs_person');

// Check ensure event is installed and install it if not - required for
// the successful operation of most functions called from this point on


function ncs_event_post_type() {
 
    $theme_name = wp_get_theme();
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Events','Post Type General Name',$theme_name ),'singular_name'       => _x( 'Event','Post Type Singular Name','menu_name'           => __( 'Events','parent_item_colon'   => __( 'Parent Event','all_items'           => __( 'All Events','view_item'           => __( 'View Event','add_new_item'        => __( 'Add New Event','add_new'             => __( 'Add New','edit_item'           => __( 'Edit Event','update_item'         => __( 'Update Event','search_items'        => __( 'Search Event','not_found'           => __( 'Not Found','not_found_in_trash'  => __( 'Not found in Trash',);
     
     $supports = array(
        'title','editor','thumbnail','comments','revisions','custom-fields',);
// Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'events','description'         => __( 'County Events','labels'              => $labels,'supports'            => $supports,'hierarchical'        => true,'public'              => true,'show_ui'             => true,'show_in_menu'        => true,'show_in_nav_menus'   => true,'show_in_admin_bar'   => true,'register_Meta_Box_cb' => 'add_custom_Meta_Box','menu_position'       => 5,'can_export'          => true,'has_archive'         => true,'exclude_from_search' => false,'publicly_queryable'  => true,'capability_type'     => 'post','show_in_rest' => true,);
     
    // Registering your Custom Post Type
    register_post_type( 'events',$args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init','ncs_event_post_type',0 );
 
/** this sets up the Box of variables **/
function add_custom_Meta_Box()
{
    add_Meta_Box( 
          $id = 'events-Meta-Box',$title = 'County Event Details',$callback = 'events_Meta_Box_format',$page = 'events',$context = 'side',$priority = 'high',$callback_args = null);
}

add_action("add_Meta_Boxes","add_custom_Meta_Box");


function events_Meta_Box_format($object)
{
    wp_nonce_field(basename(__FILE__),"Meta-Box-nonce");
    ?>
        <div>
            <label for="Meta-Box-text">Text</label>
            <input name="Meta-Box-text" type="text" value="<?PHP echo get_post_meta($object->ID,"Meta-Box-text",true); ?>">

            <br>
            <label for="location">Location</label>
            <select>
            <?PHP


                   $location_array = array(grab_locations());
                    foreach($location_array as $key => $value) 
                    {
                        if($value == get_post_meta($object->ID,"location",true))
                        {
                            ?>
                                <option selected><?PHP echo $value; ?></option>
                            <?PHP    
                        }
                        else
                        {
                            ?>
                                <option><?PHP echo $value; ?></option>
                            <?PHP
                        }
                    }
                ?>
            </select>



 
            <br>

            <label for="Meta-Box-checkBox">Check Box</label>
            <?PHP
                $checkBox_value = get_post_meta($object->ID,"Meta-Box-checkBox",true);

                if($checkBox_value == "")
                {
                    ?>
                        <input name="Meta-Box-checkBox" type="checkBox" value="true">
                    <?PHP
                }
                else if($checkBox_value == "true")
                {
                    ?>  
                        <input name="Meta-Box-checkBox" type="checkBox" value="true" checked>
                    <?PHP
                }
            ?>
        </div>
    <?PHP  
}




function grab_locations() {

        // get all the locations
    $ncs_locations = $wpdb->get_results($wpdb->prepare("SELECT nl_location_id,nl_location_name FROM " . NCS_LOCATION_TABLE  ));

    $ncs_locations_array = array();
 
    // if there are some locations add them to the array
    if ( !empty($ncs_locations) )
    {
       foreach ( $ncs_locations as $ncs_location )
       {
         array_push($ncs_locations_array,$ncs_location->nl_location_name);
       }
    }
    return $ncs_locations_array;
} //end of grab locations


// add new bits above here

?>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)