2016-11-21 32 views
0

我使用自定義元盒,第一次,我有麻煩找出並瞭解如何在框中顯示的數據。我的箱子出現在帖子中,並將其保存數據,但我不知道如何來顯示它。這裏就是我試圖完成:如何自定義顯示元后的箱數據

用於創建自定義的元框的functions.php代碼:

function custom_meta_box_markup($object) 
{ 
    wp_nonce_field(basename(__FILE__), "meta-box-nonce"); 

    ?> 
     <div> 
      <label for="meta-box-checkbox">Display CTA Area?</label> 
      <?php 
       $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true); 

       if($checkbox_value == "") 
       { 
        ?> 
         <input name="meta-box-checkbox" type="checkbox" value="true"> 
        <?php 
       } 
       else if($checkbox_value == "true") 
       { 
        ?> 
         <input name="meta-box-checkbox" type="checkbox" value="true" checked> 
        <?php 
       } 
      ?> 
      <br> 

      <label for="meta-box-text">Call to Action Title</label> 
      <input name="meta-box-text" type="text" value="<?php echo get_post_meta($object->ID, "meta-box-text", true); ?>"> 

      <br> 

      <label for="meta-box-button">CTA Button Text</label> 
      <input name="meta-box-button" type="text" value="<?php echo get_post_meta($object->ID, "meta-box-button", true); ?>"> 

     </div> 
    <?php 
} 

function add_custom_meta_box() 
{ 
    add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "post", "normal", "high", null); 
} 

add_action("add_meta_boxes", "add_custom_meta_box"); 

function save_custom_meta_box($post_id, $post, $update) 
{ 
    if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__))) 
     return $post_id; 

    if(!current_user_can("edit_post", $post_id)) 
     return $post_id; 

    if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) 
     return $post_id; 

    $slug = "post"; 
    if($slug != $post->post_type) 
     return $post_id; 

    $meta_box_text_value = ""; 
    $meta_box_checkbox_value = ""; 
    $meta_box_button_value = ""; 

    if(isset($_POST["meta-box-text"])) 
    { 
     $meta_box_text_value = $_POST["meta-box-text"]; 
    } 
    update_post_meta($post_id, "meta-box-text", $meta_box_text_value); 

    if(isset($_POST["meta-box-button"])) 
    { 
     $meta_box_text_value = $_POST["meta-box-button"]; 
    } 
    update_post_meta($post_id, "meta-box-button", $meta_box_text_value); 

    if(isset($_POST["meta-box-checkbox"])) 
    { 
     $meta_box_checkbox_value = $_POST["meta-box-checkbox"]; 
    } 
    update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value); 
} 

add_action("save_post", "save_custom_meta_box", 10, 3); 

而且這是我想獲得在模板中顯示的數據:

<div class="footer"> 
<div class="col-md-12"> 
    <h1><!--display "meta-box-text"--></h1> 
    <button><!--display "meta-box-button"--></button> 
</div> 

回答

0

使用get_post_meta($ POST_ID, 「元框文本」,真正的);您的文章循環內

相關問題