2016-07-19 114 views
0

我被客戶請求添加一個自定義字段,他們將能夠在URL中輸入。該帖子本身是一個自定義插件自定義後的類型,這是代碼我對這個部分:如何將自定義字段添加到WordPress插件

register_post_type('storylist', 
    array(
     'labels' => $labels, 
     'public' => false, 
     'exclude_from_search' => true, 
     'publicly_queryable' => false, 
     'show_ui' => true, 
     'supports' => array('title'), 
    ) 
); 
    add_filter('rwmb_meta_boxes', 'c_register_meta_boxes'); 

} 

function c_register_meta_boxes($boxes){ 
    $prefix = 'c_rwmb_'; 
    $boxes[] = array(
    'id' => 'view', 
    'title' => __('View Link', 'c_rwmb'), 
    'post_types' => array('storylist'), 
    'context' => 'normal', 
    'priority' => 'high', 
    'fields' => array(
     array(
      'name' => __('View URL', 'c_rwmb'), 
      'id' => $prefix . 'view_url', 
      'type' => 'text', 
      'size' => 60, 
      'clone' => false 
     ), 
    ) 

); 

    return $meta_boxes; 
} 

現在的問題是,當我去到了後,我沒有看到自定義元現場甚至出現了,有什麼我失蹤?

+0

愚蠢的問題,但只是可以肯定 - 你已經安裝了[Meta Box插件](https://wordpress.org/plugins/meta-box/),對不對?我很確定'rwmb_meta_boxes'是特定於它的。 – Hobo

+2

而看着代碼,你應該返回'$盒',而不是'$ meta_boxes' – Hobo

+0

@Hobo你是對的,我很傻。謝謝。 – MikeL5799

回答

0

自定義帖子類型(「storylist」)來自插件的權利?然後,您不需要再次註冊自定義帖子。您只需爲此帖子類型添加元字段並在更新帖子時保存其值。一旦我有使用自定義字段啓用/禁用邊欄的體驗。我分享了我的代碼。希望這會幫助你。

<?php 
add_action('admin_init','add_metabox_post_sidebar'); 
add_action('save_post','save_metabox_post_sidebar'); 
/* 
* Funtion to add a meta box to enable/disable the posts. 
*/ 
function add_metabox_post_sidebar() 
{ 
    add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high"); 
} 

function enable_sidebar_posts(){ 
    global $post; 
    $check=get_post_custom($post->ID); 
    $checked_value = isset($check['post_sidebar']) ? esc_attr($check['post_sidebar'][0]) : 'no'; 
    ?> 

    <label for="post_sidebar">Enable Sidebar:</label> 
    <input type="checkbox" name="post_sidebar" id="post_sidebar" <?php if($checked_value=="yes"){echo "checked=checked"; } ?> > 
    <p><em>(Check to enable sidebar.)</em></p> 
    <?php 
} 

/* 
* Save the Enable/Disable sidebar meta box value 
*/ 
function save_metabox_post_sidebar($post_id) 
{ 
    // Bail if we're doing an auto save 
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; 

    // if our current user can't edit this post, bail 
    if(!current_user_can('edit_post')) return; 

    $checked_value = isset($_POST['post_sidebar']) ? 'yes' : 'no'; 
    update_post_meta($post_id, 'post_sidebar', $checked_value); 


} 

?> 

在這裏,我添加了名爲「post_sidebar」爲崗位類型「後」的自定義字段,您可以更改自己和「後」到「storylist」在這一行add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high");更改自己的信息類型。

+0

謝謝@Palanivelrajan。我認爲這會有所幫助,我只需修改它只是一個文本框而不是複選框。 – MikeL5799

相關問題