如何从 WordPress 中的元框获取信息并将其显示在页面模板中

How to get information from meta box in WordPress and show it in a page template

我在回显模板页面上的元框元数据时遇到问题。数据好像有保存,我在页面回显数据哪里错了?

我放入检查变量是否已设置的 'hello' 会自动显示在页面上,但不会显示变量本身。我尝试了不同的地方来保存 functions.php 中的信息,但我知道的地方似乎是唯一将信息保存到其中的地方。那里有什么问题吗?或者我是如何尝试在页面上回应它们的?

这是我模板方面的 HTML 代码。 (第-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 );
}

我希望变量与放入元框中的信息一起打印。显示设施的开放时间。

请确保 $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>

我已经放了代码;

$mf = get_post_meta( get_the_ID(), 'openinghours_mf_input_field', true );
$ls = get_post_meta( get_the_ID(), 'openinghours_ls_input_field', true );

函数里面,本来应该是在WordPress循环里面的,昨晚终于弄明白了,只是忘记在这里post了。谢谢大家看我的问题!

所以改成这样;

<aside class="facility-content-sidebar">
  <?php
      $mf = get_post_meta( get_the_ID(), 'openinghours_mf_input_field', true );
      $ls = get_post_meta( get_the_ID(), 'openinghours_ls_input_field', true );

      if ( isset( $mf ) && '' !== $mf ) {
        echo 'hello';
        echo esc_attr( $mf );
     }
     if ( isset( $ls ) && '' !== $ls ) {
        echo 'hello';
        echo esc_attr( $ls );
     }
  ?>
</aside>