【问题标题】:Can't Add Custom Meta Box to Custom Post Type无法将自定义元框添加到自定义帖子类型
【发布时间】:2014-08-29 19:40:04
【问题描述】:

尝试将自定义元框添加到 wordpress 中的自定义帖子类型,但似乎没有任何效果。

当我从教程中复制并粘贴整个代码 sn-p 时,它工作得很好。当我尝试将其添加到我已经制作的自定义帖子类型中时,我什么也没得到。页面没有中断,自定义元框只是不显示。当我把头发拉出来时,希望得到一些帮助。

此时,我只想在帖子编辑界面中显示该死的东西!

// Register Custom Post Type
function generate_shows() {

  $labels = array(
    'name'                => _x( 'Shows', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Show', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Shows', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
    'all_items'           => __( 'All Shows', 'text_domain' ),
    'view_item'           => __( 'View Item', 'text_domain' ),
    'add_new_item'        => __( 'Add New Show', 'text_domain' ),
    'add_new'             => __( 'Add New', 'text_domain' ),
    'edit_item'           => __( 'Edit Item', 'text_domain' ),
    'update_item'         => __( 'Update Item', 'text_domain' ),
    'search_items'        => __( 'Search Shows', 'text_domain' ),
    'not_found'           => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' )
  );
  $args = array(
    'label'               => __( 'enk_show', 'text_domain' ),
    'description'         => __( 'An individual ENK Shows', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
    'taxonomies'          => array( 'category', 'post_tag' ),
    'hierarchical'        => true,
    'rewrite'             => array( 'slug' => 'shows', 'with_front' => false ),
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'register_meta_box_cb' => 'add_enk_metaboxes',
    'capability_type'     => 'post'
  );
  register_post_type( 'enk_show', $args );

}

// Hook into the 'init' action
add_action( 'init', 'generate_shows', 0 );


// Add the Events Meta Boxes
function add_enk_metaboxes() {
  add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'events', 'side', 'default');
}

【问题讨论】:

    标签: php wordpress wordpress-theming custom-post-type


    【解决方案1】:
    add_action('add_meta_boxes', 'add_enk_metaboxes')
    

    你还需要为盒子的html创建一个函数

    例如

    function wpt_events_location($post) { // your 3rd parameter is the function callback
         $metavalue= get_post_meta($post->id,'metakey',true);
         echo '<input type="text" name="formfield" value="'.$metavalue.'" >';
    }
    

    以及保存回调

    function save_post_meta($post_id){
      if($_POST['formfield'])
      update_post_meta($post_id,'metakey',$_POST['formfield'];
    
    }
    
    add_action('save_post', 'save_post_meta');
    

    我记得的 codex 有一些很好的例子,包括 nonces 和 OOP 风格。 PS注意上面没有安全性,您仍然需要清理值等。

    【讨论】:

      【解决方案2】:

      你必须试试这个:

      首先你为你的帖子类型创建一个元框

      <?php    
      // Add the Events Meta Boxes
          function add_enk_metaboxes($post) {
            add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'events', 'side', 'default');
          }
          add_action('add_meta_boxes','add_enk_metaboxes');
      

      然后你为元字段创建一个函数:

      function wpt_events_location($post){
      
          $txtEventLocation = get_post_meta($post->ID, 'txtEventLocation', true);
      ?>
          <table width="100%" border="0" cellspacing="4" cellpadding="0">
              <tr>
                  <td width="16%">
                      <strong>Event Location:</strong>
                  </td>
                  <td width="84%">
                      <input type="text" name="txtEventLocation" id="txtEventLocation" size="72%" value="<?php echo $txtEventLocation ?>" />
                  </td>
              </tr>
          </table>
      <?php
      }
      

      然后你保存元字段数据:

      add_action('save_post','save_event_location');
      function save_event_location(){
          global $post;
      
          $txtEventLocation = $_POST['txtEventLocation'];
      
          update_post_meta( $post->ID, 'txtEventLocation', $txtEventLocation);
      }
      
      ?>
      

      希望您能找到解决方案。

      【讨论】:

      • 快速提问,wpt_events_location 的第 2 行,您从哪里获得元键名称 txtEventLocation?我没有看到你在其他功能中使用它?
      • wpt_events_location 是元框的 id,它从未使用过......它只是为了唯一性......请你告诉我,你是什么意思(你从哪里得到元键名txtEventLocation)
      • 你没有提到它?所以我有点困惑那个字符串来自哪里以及它是如何获得元值的?
      • 在哪部分...兄弟请看好
      • 对不起 XD get_post_meta 第二个属性是关键,那不是wp_events_location 吗? :S
      【解决方案3】:

      代码将帮助您在 wordpress 中创建自定义元框

      <?php
      
      add_action( 'add_meta_boxes', 'add_custom_metaboxes' );
      
      
      // Add the Events Meta Boxes
      
      function add_custom_metaboxes() {
          //add_meta_box('wpt_events_date', 'Event Date', 'wpt_events_date', 'wpstore', 'side', 'default');
          add_meta_box('wpt_events_location', 'Address Location', 'wpt_events_location', 'post_tyme_name', 'normal', 'high');
      }
      
      // The Event Location Metabox
      
      function wpt_events_location() {
          global $post;
      
          // Noncename needed to verify where the data originated
          echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
          wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
      
          // Get the location data if its already been entered
          $addressline1 = get_post_meta($post->ID, '_addressline1', true);
          $addressline2 = get_post_meta($post->ID, '_addressline2', true);
      
      
          // Echo out the field
              echo '<p>Address Line 1:</p>';
              echo '<input type="text" name="_addressline1" value="' . $addressline1  . '" class="widefat" />';
              echo '<p>Address Line 2</p>';
              echo '<input type="text" name="_addressline2" value="' . $addressline2  . '" class="widefat" />';
      }
      
      
      // Save the Metabox Data
      
      function wpt_save_events_meta($post_id, $post) {
      
          // verify this came from the our screen and with proper authorization,
          // because save_post can be triggered at other times
          if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
          return $post->ID;
          }
      
          // Is the user allowed to edit the post or page?
          if ( !current_user_can( 'edit_post', $post->ID ))
              return $post->ID;
      
          // OK, we're authenticated: we need to find and save the data
          // We'll put it into an array to make it easier to loop though.
      
          $events_meta['_addressline1'] = $_POST['_addressline1'];
          $events_meta['_addressline2'] = $_POST['_addressline2'];
      
          // Add values of $events_meta as custom fields
      
          foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
              if( $post->post_type == 'revision' ) return; // Don't store custom data twice
              $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
              if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
                  update_post_meta($post->ID, $key, $value);
              } else { // If the custom field doesn't have a value
                  add_post_meta($post->ID, $key, $value);
              }
              if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
          }
      
      }
      
      add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields
      

      【讨论】:

        猜你喜欢
        • 2011-11-19
        • 2013-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-29
        • 2019-10-09
        • 2012-12-03
        • 1970-01-01
        相关资源
        最近更新 更多