将元框关联到 WordPress 中的自定义页面模板

Associate a Metabox to a custom page template in wordpress

本文关键字:自定义 关联 WordPress      更新时间:2023-09-26

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

这是我在函数中的代码.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');

快速说明:如果您转到具有默认页面模板的页面并从模板下拉列表中选择"关于",则必须首先保存页面才能显示元框(即,在您更改下拉列表的那一刻它不会出现)。我不相信这对你来说会是一个问题,但我认为值得一提。