zoukankan      html  css  js  c++  java
  • 黄聪:wordpress后台发布文章,自定义栏目支持HTML,可插入图片

    在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);

    效果:

  • 相关阅读:
    前端agl分页的写法
    分布式项目spring 配置文件的约束
    电脑维修常用硬件技术
    电脑维修常用检修软件技术
    电脑维修快速入门
    电脑维修基本流程
    re模块
    flask-本地线程-请求上下文补充
    SEO(搜索引擎优化)
    前端基础之jquery
  • 原文地址:https://www.cnblogs.com/huangcong/p/2592895.html
Copyright © 2011-2022 走看看