首页 开发指南

有时候,我们自定义的文章类型中,需要在快速编辑里面增加一些维护项,以方便我们的客户操作,本文针对这个问题来作解答!

这里就要用到接口quick_edit_custom_box。

”步,我们需要在后台的文章列表中增加一个columns,用到:manage_myproducts_posts_columns,其中myproducts是我们自定义文章类型的名字。代码如下:


add_filter('manage_myproducts_posts_columns', 'myown_add_post_columns');
function myown_add_post_columns($columns) {
$columns['best_sellers'] = 'Best Sellers';
$columns['hot_new'] = 'Hot New Releases';
$columns['color_price']='colors and price';
return $columns;
}

这里面我们增加了三个字段,且不论它的用途,西方只介绍如何操作这些字段。

接下来,我们要给这个字段赋值,当我们进入这个列表的时候,从数据库查询出来它有没有值,有则显示,无则为空,代码如下:


add_action('manage_myproducts_posts_custom_column', 'myown_render_post_columns', 10, 2);
function myown_render_post_columns($column_name, $id) {
switch ($column_name) {
case 'best_sellers':
$best_sellers = get_post_meta( $id, 'best_sellers', TRUE);
echo
“.$best_sellers.”;
break;
case ‘hot_new’:
$hot_new = get_post_meta( $id, ‘hot_new’, TRUE);
echo ”

“.$hot_new.”

“;
break;
case ‘color_price’:
$color_price=get_post_meta($id, ‘myproductscolor’, true);
echo “

“.$color_price.”

“;
break;
}
}

这里用了switch,当然用IF也是没有问题的。

接下来的第二步就比较重要了,在快速编辑项里面增加编辑选项。

第二步代码如下:


add_action('quick_edit_custom_box', 'myown_add_quick_edit', 10, 2);
function myown_add_quick_edit($column_name, $post_type) {
global $post;
switch ($column_name){
case 'best_sellers':

<fieldset class=”inline-edit-group inline-edit-col-left”&gt<label class=”alignleft”&gt
<input name=”best_sellers[]” type=”checkbox” value=”Yes” /&gt
<span class=”checkbox-title”>Best Sellers</span&gt
</label&gt</fieldset&gt



<fieldset class=”inline-edit-group inline-edit-col-left”&gt <input name=”hot_new[]” type=”checkbox” value=”Yes” /&gt
<span class=”checkbox-title”&gtHot New Releases</span&gt
</label&gt</fieldset&gt
<fieldset class=”inline-edit-group wp-clearfix”><span class=””>Colors and price</span>
<textarea style=”display: block; height: 4em; margin: 12px 0 0; width: ”;” name=”myproductscolor”></textarea>

示例:#000000|文章ID|http://www.163.com(颜色值是以#号开头,6位,以|隔开,文章ID为其它颜色的文章ID,链接为当前颜色的购买地址)</fieldset>
break; case 'color_price': //$color_price=get_post_meta($post->ID, 'myproductscolor', true);<


break;
}
}

这里面就有一个问题,有些编辑项里面是有值的,但是myown_add_quick_edit函数里面确没有文章ID这样的参数,单靠PHP是无法获取这个值得。经过多翻查阅资料,我发现,原来这个值是通过JS来获取得。我们先来看下快速编辑的默认项里面的源代码:

...

 

这个tr中ID的数字就是文章的ID,这样以来思路就明晰了。

引入外部JS文件:


add_action( 'admin_print_scripts-edit.php', 'rachel_carden_enqueue_edit_scripts' );
function rachel_carden_enqueue_edit_scripts() {
wp_enqueue_script( 'rachel-carden-admin-edit', get_template_directory_uri() . '/js/quick_edit.js', array( 'jquery', 'inline-edit-post' ), '', true );
}

”后quick_edit.js的代码如下:


(function($) {

// we create a copy of the WP inline edit post function
var $wp_inline_edit = inlineEditPost.edit;
// and then we overwrite the function with our own code
inlineEditPost.edit = function( id ) {

// "call" the original WP edit function
// we don't want to leave WordPress hanging
$wp_inline_edit.apply( this, arguments );

// now we take care of our business

// get the post ID
var $post_id = 0;
if ( typeof( id ) == 'object' )
$post_id = parseInt( this.getId( id ) );
if ( $post_id > 0 ) {

// define the edit row
var $edit_row = $( '#edit-' + $post_id );

// get the release date
var $release_date = $( '#products-' + $post_id ).text();
var $best_sellers=$("#best_sellers-"+$post_id).text()
var $hot_new=$("#hot_new-"+$post_id).text();
//alert($best_sellers);
if($best_sellers=="Yes"){
$edit_row.find("input[name='best_sellers[]']" ).attr("checked","checked");
}
if($hot_new=="Yes"){
$edit_row.find("input[name='hot_new[]']" ).attr("checked","checked");
}
$edit_row.find( 'textarea' ).text( $release_date );

}

};

})(jQuery);

 

”后一步就是保存数据了,代码如下:


add_action('save_post', 'myown_save_quick_edit_data');
function myown_save_quick_edit_data($post_id) {
// Authentication passed now we save the data
$best_sellers = _post('best_sellers');
$hot_new=_post("hot_new");
$myproductscolor = _post('myproductscolor') ;
//echo $myproductscolor;
//exit;
if(count($best_sellers)){
$best_sellers="Yes";
}
else{
$best_sellers="No";
}
if(count($hot_new)){
$hot_new="Yes";
}
else{
$hot_new="No";
}

if ($best_sellers)
update_post_meta( $post_id, 'best_sellers', $best_sellers);
if ($hot_new)
update_post_meta( $post_id, 'hot_new', $hot_new);

update_post_meta($post_id,'myproductscolor', $myproductscolor);
}

希望能够帮到大家!
quick_edit_custom_box 官方文档
https://codex.wordpress.org/Plugin_API/Action_Reference/quick_edit_custom_box

发表回复

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

会员评论:(0)

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