将Metabox与wordpress中的自定义页面模板相关联

时间:2015-03-08 14:57:43

标签: javascript php jquery wordpress

我正在为wordpress制作模板组合,我在自定义页面的后端添加了Metabox时遇到了一些困难。 我有一个页面"关于我"使用进度条来显示技能,我会添加一个Metabox来改变进度条的百分比,现在我可以在所有页面的后端看到Metabox并通过jquery我隐藏它如果页面的地址不是正如所料,有什么方法可以使显示更通用,并将Metabox与自定义页面模板相关联?

这是我在function.php中的代码,它运行良好,但我会使其不那么具体

<?php
add_action('add_meta_boxes', 'box_percentuale');

function box_percentuale() {

            add_meta_box(
                'percentuale',
                'Mdifica progress bar',
                'box_percentuale_style',
                'page'
            );

}


function box_percentuale_style($post) {
    ?>

        <div>
            <h4>Percentuale skills</h4>
            <label>HTML</label><input type="number" name="html" value="<?php echo get_post_meta($post->ID, 'html', true); ?>">
            <label>CSS</label><input type="number" name="css" value="<?php echo get_post_meta($post->ID, 'css', true); ?>">
            <label>JQUERY</label><input type="number" name="jquery" value="<?php echo get_post_meta($post->ID, 'jquery', true); ?>">
            <label>WORDPRESS</label><input type="number" name="wordpress" value="<?php echo get_post_meta($post->ID, 'wordpress', true); ?>">
            <label>ILLUSTRATOR</label><input type="number" name="illustrator" value="<?php echo get_post_meta($post->ID, 'illustrator', true); ?>">
            <label>PHOTOSHOP</label><input type="number" name="photoshop" value="<?php echo get_post_meta($post->ID, 'photoshop', true); ?>">
        </div>

        <script>

            jQuery(document).ready(function($) {
                var url = window.location.href;
                var urlPagina = 'http://localhost/wordpress/wp-admin/post.php?post=34&action=edit';
                $('#percentuale').css('display', 'none');
                if (url == urlPagina) {
                   $('#percentuale').show();
                };

            });

        </script>

    <?php
}

add_action('save_post', 'slava_percentuale');

function slava_percentuale($post_id)

{

    if(isset($_POST['yiw_progetti_link'])) {

        update_post_meta($post_id, 'html', intval($_POST['html']));

        update_post_meta($post_id, 'css', intval($_POST['css']));

        update_post_meta($post_id, 'jquery', intval($_POST['jquery']));

        update_post_meta($post_id, 'wordpress', intval($_POST['wordpress']));

        update_post_meta($post_id, 'illustrator', intval($_POST['illustrator']));

        update_post_meta($post_id, 'photoshop', intval($_POST['photoshop']));



    }

}

?>

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容实现:

function box_percentuale() {
    global $post;

    // This will check if you are actually on the page's editing screen
    if ( ! empty( $post ) ) {

        // This will get the page template of the page you are editing
        $page_template = get_post_meta( $post->ID, '_wp_page_template', true );

        // Check if you are using the template you wish to add meta box to
        // Simply change "about.php" to the name of your template
        // Note: if the template is in a subdirectory, you have to add it as well
        // Example: 'page-templates/about.php'
        if ( $page_template == 'about.php' ) {
            add_meta_box(
                'percentuale',
                'Mdifica progress bar',
                'box_percentuale_style',
                'page'
            );
        }
    }
}
add_action('add_meta_boxes', 'box_percentuale');

快速注意:如果您转到带有默认页面模板的页面并从模板下拉列表中选择“关于”,则必须首先保存页面,以便显示元框(即,它不会在您更改下拉列表时出现。我不相信这会是你的问题,但我认为值得一提。