我使用下面的代码将复选框添加为自定义字段。
通过复选框选择的文本将被视为“特殊文本”,其他文本将保留为“普通文本”。
<?php
add_action("admin_init", "checkbox_init");
function checkbox_init(){
add_meta_box("checkbox", "Checkbox", "checkbox", "post", "side", "high");
}
function checkbox(){
global $post;
$custom = get_post_custom($post->ID);
$field_id = $custom["field_id"][0];
echo \'<label>Check for yes</label>\';
$field_id_value = get_post_meta($post->ID, \'field_id\', true);
if($field_id_value == "yes") {
$field_id_checked = \'checked="checked"\';
}
echo \' <input type="checkbox" name="field_id" value="yes" \'.$field_id_checked.\' />\';
}
// Save Meta Details
add_action(\'save_post\', \'save_details\');
function save_details(){
global $post;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post->ID;
}
update_post_meta($post->ID, "field_id", $_POST["field_id"]);
}
?>
对于添加类别的字段中的列表,我使用以下代码:<?php
if( is_category($categories->cat_ID) ) {
if(!$wp_query) {
global $wp_query;
}
$args = array(
\'meta_key\' => \'field_id\',
\'meta_value\' => \'yes\',
);
query_posts( array_merge( $args , $wp_query->query ) );
}
if (have_posts()) : while (have_posts()) : the_post(); ?>
/***/ - posts - /***/
<?php endwhile; else: ?>
<?php endif; ?>
因此,通过复选框选择的文本被指定为“是”,我可以列出它们。但我无法列出未选中的文本。我认为他们在默认情况下是不存在的。我尝试了“meta\\u value”=>“”,或“compare”=>“NOT EXISTS”,但不起作用。我不擅长Wordpress,而且我的英语也不是很好。你能简单地解释一下吗?非常感谢。