我在使用“add post meta”和“get post meta”时遇到了问题:我在分类法中有一个新的meta框:我总是在输入字段中有“empty meta”的值,即使在我输入任何内容之后:比如“banana”,我发布文章,同样的事情:在字段中还有一次“empty meta”。。。(是不是$post->id有问题?我从来都不知道$post是否会被“理解”,这取决于它的使用方式)
/* Prints the box content */
function myplugin_inner_custom_box() {
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );
$met = get_post_meta($post->ID, \'champ\', true)? get_post_meta($post->ID, \'champ\', true) : \'empty meta\';
/* input */
echo \'<label for="myplugin_new_field">\';
_e("Description field : ", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<input type="text" id="champ" name="champ" value="\'.$met.\'" />\';
此外,我试图让meta出现在我的页面中,因此我将:
<?php
if ( get_post_meta($post->ID, \'champ_key\', true) ) {
$meta = get_post_meta($post->ID, "champ_key", true );
} else {
$meta = \'allo\';
}
?>
但页面上没有显示任何内容。
你知道我怎样才能继续写那些博文吗?
谢谢
EDIT
下面是完整的代码:如果有人可以尝试,我可能会做错什么,但我找不到什么。(整个代码都在小部件页面中)
/*
*
* add meta box : habillage
*
*/
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box\' );
add_action( \'save_post\', \'myplugin_save_postdata\' );
/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
\'test1\',
__( \'My test1\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'post\'
);
add_meta_box(
\'test2\',
__( \'My test2\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'movies\'
);
}
/* Prints the box content */
function myplugin_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );
$met = get_post_meta($post->ID, \'champ\', true)? get_post_meta($post->ID, \'champ\', true) : \'empty meta\';
/* input */
echo \'<label for="myplugin_new_field">\';
_e("Description field : ", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<input type="text" id="champ" name="champ" value="\'.$met.\'" />\';
/* select list */
echo \'<label for="my_list_field">\';
_e("Description for this field", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<select name="my_list_field" id="my_list_field">\';
print_r ($post->ID);
global $post;
$s_query = new WP_Query( array(
\'suppress_filters\' => false,
\'post_type\' => \'movies\'));
while($s_query->have_posts()):$s_query->the_post();
$sname = $post->post_title;
$s_output2 =\'\';
$s_output2 .= \'<option value="\'.$post->ID.\'" >\';
$s_output2 .= $post->post_title.\' : allo\';
$s_output2 .= \'</option>\';
echo $s_output2;
endwhile ;
echo \'</select>\';
wp_reset_query();
}
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// Check permissions
if ( \'page\' == $_POST[\'post_type\'] )
{
if ( !current_user_can( \'edit_page\', $post_id ) )
return;
}
else
{
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
}
// OK, we\'re authenticated: we need to find and save the data
//$mydata = $_POST[\'myplugin_new_field\'];
// Do something with $mydata
// probably using add_post_meta(), update_post_meta()
add_post_meta($post->ID, "champ_key", \'banana\');
return $post_id;
//return $mydata;
}
?>
SO网友:Paul_p
下面是完整的代码:如果有人可以尝试,我可能会做错什么,但我找不到什么。(整个代码都在小部件页面中)
/*
*
* add meta box : habillage
*
*/
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box\' );
add_action( \'save_post\', \'myplugin_save_postdata\' );
/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
add_meta_box(
\'test1\',
__( \'My test1\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'post\'
);
add_meta_box(
\'test2\',
__( \'My test2\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'movies\'
);
}
/* Prints the box content */
function myplugin_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );
$met = get_post_meta($post->ID, \'champ\', true)? get_post_meta($post->ID, \'champ\', true) : \'empty meta\';
/* input */
echo \'<label for="myplugin_new_field">\';
_e("Description field : ", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<input type="text" id="champ" name="champ" value="\'.$met.\'" />\';
/* select list */
echo \'<label for="my_list_field">\';
_e("Description for this field", \'myplugin_textdomain\' );
echo \'</label> \';
echo \'<select name="my_list_field" id="my_list_field">\';
print_r ($post->ID);
global $post;
$s_query = new WP_Query( array(
\'suppress_filters\' => false,
\'post_type\' => \'movies\'));
while($s_query->have_posts()):$s_query->the_post();
$sname = $post->post_title;
$s_output2 =\'\';
$s_output2 .= \'<option value="\'.$post->ID.\'" >\';
$s_output2 .= $post->post_title.\' : allo\';
$s_output2 .= \'</option>\';
echo $s_output2;
endwhile ;
echo \'</select>\';
wp_reset_query();
}
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// Check permissions
if ( \'page\' == $_POST[\'post_type\'] )
{
if ( !current_user_can( \'edit_page\', $post_id ) )
return;
}
else
{
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
}
// OK, we\'re authenticated: we need to find and save the data
//$mydata = $_POST[\'myplugin_new_field\'];
// Do something with $mydata
// probably using add_post_meta(), update_post_meta()
add_post_meta($post->ID, "champ_key", \'banana\');
return $post_id;
//return $mydata;
}
?>
SO网友:chrisguitarguy
好吧,这里有几个问题。
首先,在元框回调中(myplugin_inner_custom_box
), 你不需要把帖子全球化。WordPress将post对象作为第一个参数传递给该函数。所以你可以稍微改变一下:
<?php
function myplugin_inner_custom_box( $post ) {
// blah blah blah
}
获取post meta的三元语句是不必要的。如果给定的元键尚未设置,get\\u post\\u meta将返回一个空字符串。
$met = get_post_meta($post->ID, \'champ\', true);
现在,在保存函数中,您可能应该使用update\\u post\\u meta而不是add\\u post\\u meta。现在已经设置好了,add\\u post\\u meta将继续使用相同的键添加多个meta值,这可能不是您想要的。
此外,您将使用$post_id
, 这是您在新更新的post meta中传递到saving函数的,而不是$post->ID。
所以
<?php
function myplugin_save_postdata( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
if ( ! isset( $_POST[\'myplugin_noncename\'] ) || ! wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) )
return;
if ( \'page\' == $_POST[\'post_type\'] )
{
if ( !current_user_can( \'edit_page\' ) )
return;
}
else
{
if ( !current_user_can( \'edit_post\' ) )
return;
}
if( isset( $_POST[\'champ\'] ) )
update_post_meta( $post_id, "champ", $_POST[\'champ\'] );
}
在上面的代码中,您使用名称“champ\\u key”保存元密钥,然后尝试使用密钥“champ”检索它。不能那样做!用相同的键保存并抓取帖子元。
您的代码,修订版:http://pastie.org/2438831 (我拿出add\\u meta\\u box to movie call和select box,因为我没有电影帖子类型)。