英语不是我的母语,但我尽力向你描述我的问题。
我正在为WooCommerce开发一个插件,我认为这并不是真正相关的,但无论如何.
我已经设置了一个自定义的post类型,并为该post类型分配了一个custom-taxonomy .
对于每个自定义分类法术语,我都会在产品类型上创建一个metabox。每一种代谢物都是独一无二的,因为我通过了这个术语--鼻涕虫。
function add_post_meta_boxes() {
$args = array(
'type' => 'product',
'taxonomy' => 'picto_group',
'orderby' => 'term_group',
'order' => 'ASC'
);
$groups = get_categories($args);
foreach($groups as $group) {
//
$slug = $group->slug;
$name = $group->name;
$count = $group->category_count;
$desc = $group->category_description;
$catid = $group->cat_ID;
add_meta_box(
'i3_picto_'.$slug, // Unique ID
$name.' - '.$count, // Title
'i3_picto_meta_box', // Callback function
'product', // Admin page (or post type)
'normal', // Context
'low', // Priority
array($slug, $catid, $desc)// Arguments to pass into the callback function
);
}// End foreach
}这个metabox充满了所有有自定义术语的帖子.
function i3_picto_meta_box( $object, $box ) {
$slug = $box['args'][0];
$catid = $box['args'][1];
$desc = $box['args'][2];
?>
<p>
<?php
$args = array(
'post_type' => 'product_picto', // Post-Type name
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'picto_group', // Taxonomy name
'field' => 'id',
'terms' => $catid // Term ID
)
)
);
$pictograms = get_posts( $args );
foreach ( $pictograms as $pictogram ) :
setup_postdata( $pictogram );
$pslug = $pictogram->post_name;
$checked = get_post_meta($pictogram->ID, 'cb-one[cb-'.$slug.'_'.$pslug.']', true);
?>
<div class="group-<?php echo $slug; ?>-<?php echo $pslug; ?>">
<label for="cb-<?php echo $slug; ?>_<?php echo $pslug; ?>">
<?php echo get_the_post_thumbnail( $pictogram->ID, 'thumbnail' ); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<input type="checkbox" name="cb-one[cb-<?php $slug; ?>_<?php $pslug;?>']" id="cb-<?php echo $slug; ?>_<?php echo $pslug; ?>" <?php if( $checked == true ) { ?>checked="checked"<?php } ?> />
</label>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
</p>
<?php }所有帖子都显示标题、缩略图和内容。
我也会在每一篇文章中显示一个复选框。因此,当我编辑一些产品,我可以检查一些类别的帖子。最后,这些帖子也应该显示在前端。
现在,在如何为每个复选框保存状态方面,我遇到了很大的麻烦。因为我的所有metaboxes都是自动生成的,而且正如您所看到的,我的复选框字段有一个自动生成的名称“术语-段塞”+“后段塞”。
如何将生成的复选框字段名传递给我的save_post function
我想我用错误的方式命名了我的复选框字段,我还认为我需要在保存函数中的update_post_meta上运行一个foreach循环?
也许我想的太复杂了。
也许有人能理解这一点,并有一些有用的信息给我。
谢谢你,莫
更新
嘿伙计们,
所以今天我研究并更新了我的代码。我现在可以保存我的metaboxes和checked状态了。但有些事情还不太对劲。
我希望我可以编辑我的问题,而不是评论。
新的checkbox代码如下所示:
<input type="checkbox" name="cb-one[]" id="cb-<?php echo $slug; ?>_<?php echo $pslug; ?>" value="<?php echo $pid; ?>" <?php checked( in_array( $pid, $pictoarray ) ); ?> />保存metabox的函数如下所示:
function i3_save_post_class_meta( $post_ID ) {
global $post;
if(isset( $_POST['cb-one'] ))
{
$custom = $_POST['cb-one'];
$old_meta = get_post_meta($post->ID, '_cb-one');
// Update post meta
if(!empty($old_meta)){
update_post_meta($post->ID, '_cb-one', $custom );
} else {
add_post_meta($post->ID, '_cb-one', $custom );
}
}
}为了还能够获得单个复选框的checked=checked状态,我将这段代码添加到i3_picto_meta_box函数的顶部:
global $post;
$pictolist = get_post_meta( $post->ID, '_cb-one');
if (!empty($pictolist[0])) {
$pictoarray = $pictolist[0]; // Get the right array
}在这里,Iam将元数据作为数组,在输入元素上,我检查该元素的值是否也在该数组中。
有了这些更新,我现在可以在post/product中选择多个复选框。所选帖子的ID被保存,我也可以在前面检索它们。
我正在与复选框工作,因为我希望能够也取消他们在未来。
所以我试了一下,我选择了多个复选框并保存了帖子。在此之后,我取消了每个复选框,并再次保存。
不幸的是,所有的复选框都被选中了!
比我只检查了一个盒子并保存下来,一切都很好。我继续一个接一个地做这个,只是为了知道,每次最后一个复选框总是被选中。
除了一个,我可以取消所有的复选框。一旦我保存了这篇文章,就会有一个复选框被选中。
我希望有人能帮我解决这个问题。
谢谢你,莫
发布于 2015-04-17 15:01:02
几乎总是在提出问题之后,我找到了解决办法;)
我做到了。问题就在我的保存功能中。该函数被设置为在未选中复选框时添加一些元数据。
所以我把它改成了这个
/**
* Saving the meta-boxes
**/
function i3_save_post_class_meta( $post_ID ) {
global $post;
if(isset( $_POST['cb-one']))
{
$custom = $_POST['cb-one'];
$old_meta = get_post_meta($post->ID, '_cb-one');
// Update post meta
//if(!empty($old_meta)){
update_post_meta($post->ID, '_cb-one', $custom );
//}
}else{
delete_post_meta($post->ID, '_cb-one', $custom );
}
}希望这也能帮助到其他一些人。
https://stackoverflow.com/questions/29653361
复制相似问题