javascript - 将 Metabox 关联到 WordPress 中的自定义页面模板

标签 javascript php jquery wordpress

我正在为 wordpress 制作模板组合,在自定义页面后端添加 Metabox 时遇到一些困难。 我有一个“关于我”页面,带有进度条来显示技能,我会在baknd中添加一个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']));



    }

}

?>

最佳答案

您可以使用以下方法来实现:

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');

快速说明:如果您转到具有默认页面模板的页面并从模板下拉列表中选择“关于”,则必须先保存页面才能显示元框(即,它不会在您更改下拉菜单时出现)。我认为这不会成为您的问题,但我认为值得一提。

关于javascript - 将 Metabox 关联到 WordPress 中的自定义页面模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28927842/

相关文章:

javascript - 为什么 Jasmine 在监视 $.ajax 时不重置 spy ?

javascript - 如何注入(inject)javascript并修改原生安卓浏览器谷歌搜索结果

javascript - 迭代 json 对象并将每个对象(图像)附加到 HTML div 标签

PHP:UTF8 中西里尔字符串的不区分大小写的 preg_replace

javascript - 试图让一个 Div 与 JQuery 一起消失?

Javascript计算功能 - 不在html中计算

javascript - 如何将参数传递给 jQuery ajax 请求的回调?

Javascript循环迭代错误

php - Cake 1.3 中是否有任何好的/简单的 Ajax 示例而不使用已弃用的助手?

php - 在 PHP exec() 中隐藏命令参数