我在回显模板页面上的meta-box的元数据时遇到问题。数据似乎已保存,在页面上回显数据时我在哪里做错了?
T我在检查是否设置了变量的时候放入的'hello'会在页面上显示出来,而不是变量本身。我已经尝试过在函数.php中保存信息的其他地方,但是我所知道的地方似乎是将信息保存到其中的唯一地方。那里或我试图在页面上回显它们时有问题吗?
这是从我的模板角度来看的HTML代码。 (page-facility.php)
<aside class="facility-content-sidebar">
<?php
if ( isset( $mf ) && '' !== $mf ) {
echo 'hello';
echo esc_attr( $mf );
}
if ( isset( $ls ) && '' !== $ls ) {
echo 'hello';
echo esc_attr( $ls );
}
?>
</aside>
这是我在functions.php中的全部代码
/* Opening Hours meta box on template */
add_action('add_meta_boxes', 'add_openinghours_meta');
function add_openinghours_meta() {
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-facility.php' )
{
add_meta_box(
'openinghours_meta', // $id
'Öppettider', // $title
'display_opening_information', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
if ( ! function_exists( 'display_opening_information' ) ) {
/**
* Meta box render function
*
* @param object $post Post object.
* @since 1.0.0
*/
function display_opening_information($post) {
// Add the HTML for the post meta
$meta = get_post_meta( $post->ID );
$openinghours_mf_input_field = ( isset( $meta['openinghours_mf_input_field'][0] ) && '' !== $meta['openinghours_mf_input_field'][0] ) ? $meta['openinghours_mf_input_field'][0] : '';
$openinghours_ls_input_field = ( isset( $meta['openinghours_ls_input_field'][0] ) && '' !== $meta['openinghours_ls_input_field'][0] ) ? $meta['openinghours_ls_input_field'][0] : '';
wp_nonce_field( 'openinghours_control_meta_box', 'openinghours_control_meta_box_nonce' ); // Always add nonce to your meta boxes!
?>
<div class="post_meta_extras">
<p>
<label><?php esc_attr_e( 'Måndag-Fredag:', 'openinghours_mf_input_field' ); ?></label>
<input type="text" name="openinghours_mf_input_field" value="<?php echo esc_attr( $openinghours_mf_input_field ); ?>">
</p>
<p>
<label><?php esc_attr_e( 'Lördag-Söndag:', 'openinghours_ls_input_field' ); ?></label>
<input type="text" name="openinghours_ls_input_field" value="<?php echo esc_attr( $openinghours_ls_input_field ); ?>">
</p>
</div>
<?php
}
}
add_action( 'save_post', 'openinghours_save_metabox' );
if ( ! function_exists( 'openinghours_save_metabox' ) ) {
/**
* Save controls from the meta boxes
*
* @param int $post_id Current post id.
* @since 1.0.0
*/
function openinghours_save_metabox( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times. Add as many nonces, as you
* have metaboxes.
*/
if ( ! isset( $_POST['openinghours_control_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['openinghours_control_meta_box_nonce'] ), 'openinghours_control_meta_box' ) ) { // Input var okay.
return $post_id;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) { // Input var okay.
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
/*
* If this is an autosave, our form has not been submitted,
* so we don't want to do anything.
*/
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
/* Ok to save */
if ( isset( $_POST['openinghours_mf_input_field'] ) ) { // Input var okay.
update_post_meta( $post_id, 'openinghours_mf_input_field', sanitize_text_field( wp_unslash( $_POST['openinghours_mf_input_field'] ) ) ); // Input var okay.
}
if ( isset( $_POST['openinghours_ls_input_field'] ) ) { // Input var okay.
update_post_meta( $post_id, 'openinghours_ls_input_field', sanitize_text_field( wp_unslash( $_POST['openinghours_ls_input_field'] ) ) ); // Input var okay.
}
}
$mf = get_post_meta( get_the_ID(), 'openinghours_mf_input_field', true );
$ls = get_post_meta( get_the_ID(), 'openinghours_ls_input_field', true );
}
我希望使用在meta-box中的信息来打印virables。显示该设施的开放时间。
请确保在$mf
模板文件中初始化$ls
和page-facility.php
变量。
例如:
<aside class="facility-content-sidebar">
<?php
//initilize variables
$mf = get_post_meta( get_the_ID(), 'openinghours_mf_input_field', true );
$ls = get_post_meta( get_the_ID(), 'openinghours_ls_input_field', true );
//then check if empty
if ( isset( $mf ) && '' !== $mf ) {
echo 'hello';
echo esc_attr( $mf );
}
if ( isset( $ls ) && '' !== $ls ) {
echo 'hello';
echo esc_attr( $ls );
}
?>
</aside>