在新发布页面上使用下拉菜单创建自定义域

时间:2012-12-23 作者:Umesh Awasthi

我正在尝试创建一个默认自定义字段下拉列表,其中包含在创建新帖子或编辑现有帖子时显示的DB中预先填充的值。

我知道如何添加单个自定义字段,并添加了以下几个字段

add_action(\'wp_insert_post\', \'set_default_custom_fields\'); 
function set_default_custom_fields($post_id) {
if ( $_GET[\'post_type\'] != \'page\' ) {
add_post_meta($post_id, \'email\', \'[email protected]\', true);
}
}
这对我来说很好,我可以看到带有默认值的自定义字段,但我不确定如何添加下拉列表来代替单个文本字段,我尝试了类似的方法,但似乎不起作用。

 add_action(\'wp_insert_post\', \'set_default_custom_fields\'); 
  function set_default_custom_fields($post_id) {
    if ( $_GET[\'post_type\'] != \'page\' ) {
<select name="voodoo_dropdown" id="voodoo_dropdown">
        <option<?php selected( add_post_meta($post->ID, \'voodoo_dropdown\', true), \'USA\' ); ?>>USA</option>
        <option<?php selected( add_post_meta($post->ID, \'voodoo_dropdown\', true), \'Canada\' ); ?>>Canada</option>
        <option<?php selected( add_post_meta($post->ID, \'voodoo_dropdown\', true), \'Mexico\' ); ?>>Mexico</option>
        </select>
}
return ;
}
由于我缺乏知识,我不确定我做错了什么,或者需要做什么来完成这项任务

3 个回复
SO网友:dbeja

不要为自定义字段定义默认值,而应该像米洛所说的那样。在函数中应该有这样的内容。php。我试着按照你需要的做,但没有测试。

<?php

// action to add meta boxes
add_action( \'add_meta_boxes\', \'voodoo_dropdown_metabox\' );
// action on saving post
add_action( \'save_post\', \'voodoo_dropdown_save\' );

// function that creates the new metabox that will show on post
function voodoo_dropdown_metabox() {
    add_meta_box( 
        \'voodoo_dropdown\',  // unique id
        __( \'Voodoo Dropdown\', \'mytheme_textdomain\' ),  // metabox title
        \'voodoo_dropdown_display\',  // callback to show the dropdown
        \'post\'   // post type
    );
}

// voodoo dropdown display
function voodoo_dropdown_display( $post ) {

  // Use nonce for verification
  wp_nonce_field( basename( __FILE__ ), \'voodoo_dropdown_nonce\' );

  // get current value
  $dropdown_value = get_post_meta( get_the_ID(), \'voodoo_dropdown\', true );
  ?>
    <select name="voodoo_dropdown" id="voodoo_dropdown">
        <option value="USA" <?php if($dropdown_value == \'USA\') echo \'selected\'; ?>>USA</option>
        <option value="Canada" <?php if($dropdown_value == \'Canada\') echo \'selected\'; ?>>Canada</option>
        <option value="Mexico" <?php if($dropdown_value == \'Mexico\') echo \'selected\'; ?>>MEXICO</option>
    </select>
  <?php
}

// dropdown saving
function voodoo_dropdown_save( $post_id ) {

    // if doing autosave don\'t do nothing
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // verify nonce
  if ( !wp_verify_nonce( $_POST[\'voodoo_dropdown_nonce\'], basename( __FILE__ ) ) )
      return;


  // Check permissions
  if ( \'page\' == $_POST[\'post_type\'] ) 
  {
    if ( !current_user_can( \'edit_page\', $post_id ) )
        return;
  }
  else
  {
    if ( !current_user_can( \'edit_post\', $post_id ) )
        return;
  }

  // save the new value of the dropdown
  $new_value = $_POST[\'voodoo_dropdown\'];
  update_post_meta( $post_id, \'voodoo_dropdown\', $new_value );
}
?>
如果你认为这太复杂了,你可以使用一个元盒插件,在Wordpress插件目录中找到一个已经有下拉列表的插件。

SO网友:Milo

通过将下拉列表添加到元框中的后期编辑屏幕add_meta_box, 然后在save_post 操作您可以检查所选选项并将其另存为post meta。

SO网友:Yogendra

一旦我完成了类似的需求,这是一个原始代码,希望这会有所帮助。

<?php
add_action( \'add_meta_boxes\', \'yss_custom_post_cat_add\' );
function yss_custom_post_cat_add() {
 add_meta_box( \'my-meta-box-id\', \'My Custom Post Category\', \'yss_custom_post_cat\', \'post\', \'normal\', \'high\' );
}

function yss_custom_post_cat( $post ) {
 $values = get_post_custom( $post->ID );
 $selected = isset( $values[\'custom_post_cat_select\'] ) ? esc_attr( $values[\'custom_post_cat_select\'][0] ) : \'\';
 wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
?>

   <p>
    <label for="custom_post_cat_select">Select your custom Post Category</label>
    <br>
    <select name="custom_post_cat_select" id="custom_post_cat_select">
     <option value="picture" <?php selected( $selected, \'none\' ); ?>>(none)</option>
     <option value="picture" <?php selected( $selected, \'picture\' ); ?>>Picture</option>
     <option value="video" <?php selected( $selected, \'video\' ); ?>>Video</option>
     <option value="sports" <?php selected( $selected, \'sports\' ); ?>>Sports</option>
     <option value="guesswho" <?php selected( $selected, \'guesswho\' ); ?>>Guess Who</option>
     <option value="decisionpoll" <?php selected( $selected, \'decisionpoll\' ); ?>>Decision Poll</option>
     <option value="audio" <?php selected( $selected, \'audio\' ); ?>>Audio</option>
    </select>
   </p>
<?php
}

add_action( \'save_post\', \'yss_custom_post_cat_save\' );
function yss_custom_post_cat_save( $post_id ) {
 // Bail if we\'re doing an auto save
 if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
 // if our nonce isn\'t there, or we can\'t verify it, bail
 if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) return;
 // if our current user can\'t edit this post, bail
 if( !current_user_can( \'edit_post\' ) ) return;
 // now we can actually save the data
 $allowed = array(
            \'a\' => array( // on allow a tags
            \'href\' => array() // and those anchords can only have href attribute
    )
 );

// Probably a good idea to make sure your data is set
 if( isset( $_POST[\'custom_post_cat_select\'] ) )
  update_post_meta( $post_id, \'custom_post_cat_select\', esc_attr( $_POST[\'custom_post_cat_select\'] ) );
}
?>

结束

相关推荐