2016-07-19 115 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

相关问题