首页 开发指南

本文接《wordpress后台发布文章,自定义栏目支持HTML,可插入图片》。

在《wordpress后台发布文章,自定义栏目支持HTML,可插入图片》这个文章中我们实现的在wordpress后台实现真正意义上的自定义文章模型的问题。但经本人测试还是有些小问题的,先上下本人的代码:

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, 'pro_pic', true);
echo '<h4>产品图片</h4>';
wp_editor(str_replace('"','"',$meta_box_value), 'pro_pic', $settings = array(quicktags=>1,
tinymce=>1,
media_buttons=>0,
textarea_rows=>4,
editor_class=>"textareastyle") );
$meta_box_value_chisan = get_post_meta($post->ID, 'chisan', true);
echo '<h4>尺寸与散热</h4>';
wp_editor(str_replace('"','"',$meta_box_value_chisan), 'chisan', $settings = array(quicktags=>1,
tinymce=>1,
media_buttons=>0,
textarea_rows=>4,
editor_class=>"textareastyle") );
$meta_box_value_guangcanshu = get_post_meta($post->ID, 'guangcanshu', true);
echo '<h4>光参数</h4>';
wp_editor(str_replace('"','"',$meta_box_value_guangcanshu), 'guangcanshu', $settings = array(quicktags=>1,
tinymce=>1,
media_buttons=>0,
textarea_rows=>4,
editor_class=>"textareastyle") );
}

function save_postdata($post_id){
global $post;
$data_pic = wpautop($_POST['pro_pic']);
$data_chisan=wpautop($_POST['chisan']);
$data_guangcanshu=wpautop($_POST['guangcanshu']);

if(get_post_meta($post_id,'pro_pic') == "")
add_post_meta($post_id,'pro_pic', $data_pic, true);
elseif($data_pic != get_post_meta($post_id,'pro_pic', true))
update_post_meta($post_id,'pro_pic', $data_pic);
elseif($data_pic == "")
delete_post_meta($post_id,'pro_pic', get_post_meta($post_id,'pro_pic', true));

if(get_post_meta($post_id,'chisan') == "")
add_post_meta($post_id,'chisan', $data_chisan, true);
elseif($data_chisan != get_post_meta($post_id,'chisan', true))
update_post_meta($post_id,'chisan', $data_chisan);
elseif($data_chisan == "")
delete_post_meta($post_id,'chisan', get_post_meta($post_id,'chisan', true));

if(get_post_meta($post_id,'guangcanshu') == "")
add_post_meta($post_id,'guangcanshu', $data_guangcanshu, true);
elseif($data_guangcanshu != get_post_meta($post_id,'guangcanshu', true))
update_post_meta($post_id,'guangcanshu', $data_guangcanshu);
elseif($data_guangcanshu == "")
delete_post_meta($post_id,'guangcanshu', get_post_meta($post_id,'guangcanshu', true));
}

以上代码实现在添加文章中的自定义栏目功能,在编辑文章(也就是修改某个文章的时候)这个功能也正常,但是,当我们在文章列表页面,点击快速编辑的时候,自定义的这几个字段总是被清空。

关于这个问题,我查了好多资料,也看了一天这个WP的代码,我想唯一正确的方法还是从我们的FUNCTION文件着手,而不是去修改WP本身的核心代码。

这里给大家推荐一个网站:http://wordpress.stackexchange.com

基本都是关于WP的问题,不过是英文的。

本文所讲的问题,这里也有涉及到,这里摘录出来:

“Quick Edit” > update clears out my custom meta values

I have two custom meta fields that I’ve enabled for each post, scottb_customHeader and scottb_customTitle

These work fine as long as I’m using the full edit feature to edit posts. However, when I click “Quick Edit”, then click “Update”, my custom meta values for the post are cleared out. What do I need to do to resolve?

Code is below…

我们来看下正确答案:

I had the same issue. Just add the following code at the beginning of the save_post action hook callback function (the function used to save the custom data).

// handle the case when the custom post is quick edited
// otherwise all custom meta fields are cleared out
if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
      return;

What it actually does: it checks if the quick saving wp_nonce_field exists and returns if that’s the case. No need to create an additional hidden field in the form.

是的,就是这么简单,看来WP在国内的资料还是比较少,大家都在不停的汉化老外的东西,却少了些原创。

正确的函数是:

function save_postdata($post_id){
global $post;

if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
return;//我想这个就是判断是不是快速编辑的条件,假如是快速编辑就跳出函数,这样以来我们的metabox也就不会被操作了!
$data_pic = wpautop($_POST['pro_pic']);
$data_chisan=wpautop($_POST['chisan']);
$data_guangcanshu=wpautop($_POST['guangcanshu']);

if(get_post_meta($post_id,'pro_pic') == "")
add_post_meta($post_id,'pro_pic', $data_pic, true);
elseif($data_pic != get_post_meta($post_id,'pro_pic', true))
update_post_meta($post_id,'pro_pic', $data_pic);
elseif($data_pic == "")
delete_post_meta($post_id,'pro_pic', get_post_meta($post_id,'pro_pic', true));

if(get_post_meta($post_id,'chisan') == "")
add_post_meta($post_id,'chisan', $data_chisan, true);
elseif($data_chisan != get_post_meta($post_id,'chisan', true))
update_post_meta($post_id,'chisan', $data_chisan);
elseif($data_chisan == "")
delete_post_meta($post_id,'chisan', get_post_meta($post_id,'chisan', true));

if(get_post_meta($post_id,'guangcanshu') == "")
add_post_meta($post_id,'guangcanshu', $data_guangcanshu, true);
elseif($data_guangcanshu != get_post_meta($post_id,'guangcanshu', true))
update_post_meta($post_id,'guangcanshu', $data_guangcanshu);
elseif($data_guangcanshu == "")
delete_post_meta($post_id,'guangcanshu', get_post_meta($post_id,'guangcanshu', true));
}

发表回复

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

会员评论:(0)

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