2017-06-05 37 views
0

私はページテンプレートを制御する別のメタボックスを追加するカスタムプラグインを作成しました。設定値で1ページしかなければ、コードは完璧に機能します。Wordpressカスタムメタボックス更新テンプレート

例: 1ページが選択されたラジオボタン2が(正常に動作します)があり 2ページは、ラジオボタン2選択した(最初のページが2ページ目がpage.php・デフォルト・ファイルがデフォルトになります正常に動作します。)

1つのページに異なるテンプレート値を選択する理由についてのアイデアはありますが、他のすべてのテンプレート値はデフォルトでpage.phpテンプレートに戻されますか?

<?php 
/** 
* Plugin Name: Wireframe Templates For Pages V3.7 
* Description: Adds a wireframe selection section for the page template 
* Version: 1.0 
* Author: Daniel Vickers 
*/ 

// Dont call me direct! 
if (! defined('ABSPATH')) exit; 


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

    ?> 

<style> 
label > input{ /* HIDE RADIO */ 
    visibility: hidden; /* Makes input not-clickable */ 
    position: absolute; /* Remove input from document flow */ 
} 
label > input + img{ /* IMAGE STYLES */ 
    cursor:pointer; 
    border:2px solid transparent; 
} 
label > input:checked + img{ /* (RADIO CHECKED) IMAGE STYLES */ 
    border:2px solid #f00; 
} 
</style> 



     <div> 

      <h4>Radio options</h4> 
      <?php 
       // U need to use this to set the checked="checked" 
       $checkbox_value = get_post_meta($object->ID, "page-template-radio", true); 
      ?> 
         <label> 
         <input type="radio" name="page-template-radio" value="default"<?php if($checkbox_value == 'default'){echo 'checked =\"checked\"';} ?> /><img src="http://via.placeholder.com/150x220"></label> 
         <label> 
         <input type="radio" name="page-template-radio" value="2col" <?php if($checkbox_value == '2col'){echo 'checked =\"checked\"';} ?>/><img src="http://via.placeholder.com/150x220"></label> 
         <label> 
         <input type="radio" name="page-template-radio" value="1col" <?php if($checkbox_value == '1col'){echo 'checked =\"checked\"';} ?>/> 
         <img src="http://via.placeholder.com/150x220"> 
         </label> 

     </div> 
<?php 
} 


// Saving data 
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; 



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

} 

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


function add_custom_meta_box() 
{ 
    add_meta_box("demo-meta-box", "Page Template", "custom_meta_box_markup", "page", "normal", "high", null); 
} 

add_action("add_meta_boxes", "add_custom_meta_box"); 

// Get our option for post ID from the options meta box change "$field_name" to your option name you use in the meta box 
$post_option = get_post_meta(get_the_ID(),"page-template-radio",true); 

// Check our option and change the display to what option is set 

    if($post_option == "default") 
    { 
     update_post_meta($page_id, '_wp_page_template', 'page.php'); 
    } 
    elseif($post_option == "2col") { 
     update_post_meta($page_id, '_wp_page_template', 'page-2col.php'); 
    } 
    elseif ($post_option == "1col") { 
     update_post_meta($page_id, '_wp_page_template', 'page-1col.php'); 
    } 
    else{ 
    // Added for when an option is not set 
    }?> 

答えて

0

あなたのコードはちょっと混乱しているように見えますが、私は問題があると思います。 値を取得するメタプロパティとは異なるメタプロパティに値を保存しています。

編集画面では、「追加フィールド」アイテムを開くこともできます。それはあなたの投稿のすべてのメタデータを表示し、メタの保存に関する問題を診断するための大きな助けとなります。

https://github.com/svrooij/wp-geocoded-posts/blob/master/geocoded-posts/includes/class-geocoded-posts-editor.phpこれは私のプラグインのクラスで、投稿にいくつかのメタフィールドを追加するためのものです。それは、プラグインファイルによってフォルダがより高く読み込まれています。

関連する問題