个性化阅读
专注于IT技术分析

无法将自定义元框添加到自定义帖子类型

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

当我从教程中复制并粘贴整个代码片段时, 它就可以正常工作。当我尝试将其添加到已经完成的自定义帖子类型中时, 我什么也没得到。页面不会中断, 自定义元框不会显示。在我拔头发时, 希望能有所帮助。

在这一点上, 我只希望将这些东西显示在后期编辑界面中!

// 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');
}

#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');

我记得该抄本有一些很好的例子, 包括现时和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);
}

?>

希望你找到解决方案。


#3


代码将帮助你在Wordpress中创建自定义metabox

<?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
赞(0) 打赏
未经允许不得转载:srcmini » 无法将自定义元框添加到自定义帖子类型
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏