Wordpress自定义元框更新模板

时间:2017-06-05 20:01:25

标签: php html wordpress plugins

所以我创建了一个自定义插件来添加另一个控制页面模板的元框。如果您只有1页设置值,则代码功能完全正常。

实施例: 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
    }?>

1 个答案:

答案 0 :(得分:0)

你的代码看起来有点乱,但我想我看到了问题。 您将值保存在不同的元属性中,而不是从中获取值。

在编辑屏幕中,您还可以打开“额外字段”项。这将显示帖子的所有元数据,对于诊断保存元的问题是一个很好的帮助。

您还可以查看https://github.com/svrooij/wp-geocoded-posts/blob/master/geocoded-posts/includes/class-geocoded-posts-editor.php这是我的插件的类,用于向帖子添加一些元字段。它被插件文件加载到更高的文件夹。

相关问题