我正在处理的代码未返回电话号码的值
我正在尝试为商家位置创建自定义帖子
我丢失了一些东西,我看不到。
function locations_footer() {
$args = array('post_type'=>'locations',);
$location_loop = new WP_Query($args);
if($location_loop->have_posts()):
while($location_loop->have_posts()):
$location_loop->the_post();
$locations = get_post_meta($post->ID,'location_fields',true);
$telephone = $locations['telephone'];
$title = get_the_title();
$content = get_the_content();
echo'Title:'.$title.'<br>Telephone:'.$telephone.'<br>Content:'.$content.'';
endwhile;
endif;
wp_reset_postdata();
}
add_action('wp_footer','locations_footer');
function locations_post(){
register_post_type('locations', array(
'labels' => array(
'name'=>__('Locations'),
),
'public' => true,
'hierarchical' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
),
'taxonomies' => array(
'post_tag',
'category',
)
));
register_taxonomy_for_object_type('category','locations');
register_taxonomy_for_object_type('post_tag','locations');
}
add_action('init','locations_post');
function locations_meta_box(){
add_meta_box(
'locations_fields_meta_box',//$id
'Locations',//$title
'show_locations_fields_meta_box',//$callback
'locations',//$screen
'normal',//$context
'high'//$priority
);
}
add_action('add_meta_boxes','locations_meta_box');
function show_locations_fields_meta_box(){
global $post;
$locations = get_post_meta($post->ID,'locations_fields',true);
?>
<input type="hidden" name="locations_meta_box_nonce"value="<?php echo wp_create_nonce(basename(__FILE__)); ?> ">
<p>
<labelfor="locations_fields[text]">Phone</label><br>
<input type="text" name="locations_fields[telephone]" id="locations_fields[telephone]" class="regular-text" value="<?php echo $locations['telephone'];?>">
</p>
<?php
}
add_action('add_meta_boxes','show_locations_fields_meta_box');
function save_locations_fields_meta($post_id) {
if(!wp_verify_nonce($_POST['locations_meta_box_nonce'],basename(__FILE__))) {
return$post_id;
}
if(defined('DOING_AUTOSAVE')&&DOING_AUTOSAVE) {
return$post_id;
}
if('page'===$_POST['post_type']) {
if(!current_user_can('edit_page',$post_id)) {
return $post_id;
}
else if(!current_user_can('edit_post',$post_id)) {
return $post_id;
}
}
$old = get_post_meta($post_id,'locations_fields',true);
$new = $_POST['locations_fields'];
if($new&&$new!==$old) {
update_post_meta($post_id,'locations_fields',$new);
} elseif(''===$new && $old) {
delete_post_meta( $post_id,'locations_fields',$old );
}
}
add_action('save_post','save_locations_fields_meta');
答案 0 :(得分:0)
尝试此代码。我已经评论了这一行convert -size 100x100 xc:red red.png
。这将添加2个meta框。
还要更改保存的邮政编码。
//add_action('add_meta_boxes','show_locations_fields_meta_box');
答案 1 :(得分:0)
在此功能中:
function locations_footer() {
$args = array('post_type'=>'locations',);
$location_loop = new WP_Query($args);
if($location_loop->have_posts()):
while($location_loop->have_posts()):
$location_loop->the_post();
$locations = get_post_meta($post->ID,'location_fields',true);
$telephone = $locations['telephone'];
$title = get_the_title();
$content = get_the_content();
echo'Title:'.$title.'<br>Telephone:'.$telephone.'<br>Content:'.$content.'';
endwhile;
endif;
wp_reset_postdata();
}
add_action('wp_footer','locations_footer');
此行上似乎缺少“ s” $locations = get_post_meta($post->ID,'location_fields',true);
应该是$locations = get_post_meta($post->ID,'locations_fields',true);
同样,对于$post->ID
,请确保该值有效(如果可能不想尝试,请尝试以下操作):https://developer.wordpress.org/reference/functions/get_the_id/