首页 开发指南

在functions.php中添加如下代码

add_action('admin_menu', 'create_meta_box');
//在加载管理员界面界面的时候调用create_meta_box函数
add_action('save_post', 'save_postdata');

function create_meta_box(){
    if(function_exists('add_meta_box')){
        //add_meta_box函数在文章编辑页面内添加版块,具体用法放在文章”后
        add_meta_box('new-meta-box','自定义模块','new_meta_box','post','normal','high');
        //此函数调用new_meta_box函数
    }
}

function new_meta_box(){
    global $post;
    $meta_box_value = get_post_meta($post->ID, 'answer', true);
    echo '<h4>答案</h4>';
    wp_editor(str_replace('"','"',$meta_box_value), 'answer', $settings = array(quicktags=>1,
                    tinymce=>1,
                    media_buttons=>0,
                    textarea_rows=>4,
                    editor_class=>"textareastyle") );
}

function save_postdata($post_id){
    global $post;
    $data = wpautop($_POST['answer']);
    if(get_post_meta($post_id,'answer') == "")
        add_post_meta($post_id,'answer', $data, true);
    elseif($data != get_post_meta($post_id,'answer', true))
        update_post_meta($post_id,'answer', $data);
    elseif($data == "")
        delete_post_meta($post_id,'answer', get_post_meta($post_id,'answer', true));
}

前台调用代码:

echo get_post_meta($post->ID, 'answer', true);

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

会员评论:(0)

声明:本站所有主题/文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除,谢谢!
©www.sbmzenith.com 2013-2022 All Rights Reserved.
豫ICP备15009393号