2017-06-05 133 views
0

所以我创建了一个自定义插件来添加另一个元框来控制页面模板。如果您只有一页设定值,代码功能就会很好。Wordpress自定义元框更新模板

示例: 1页有选中的单选按钮2(正常工作) 2页有选中的单选按钮2

(第一页将正常工作的第二页将默认为page.php文件默认文件)。

任何想法为什么它会为一个页面选择不同的模板值,但对于其他所有其他模板默认返回到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这是我的插件的类,用于向帖子添加一些元字段。它会被插件文件加载到更高的文件夹中。