如何在管理员中加载特定页面的php文件?

时间:2013-02-20 作者:tigre

您好,谢谢您的指导!

正在尝试加载自定义。仅用于我的主页的php文件(模板为page-home.php) 使用此代码:

add_action(\'admin_init\',\'load_home_meta\');
function load_home_meta() {
  global $pagenow, $post;
  if ($pagenow==\'edit.php\' && \'104\' == $post->ID) {
        include \'metaboxes/home-meta.php\';
  }
}
页面的唯一ID为104。我只需要在主页上使用它,因为php文件中的nonce检查破坏了保存不使用这些自定义元字段的其他页面的能力。

谢谢

尝试两种方法:

尝试以下方法:

add_action(\'admin_init\',\'load_home_meta\');
function load_home_meta() {
    // Get the Post ID
    if( isset( $_GET[\'post\'] ) ) $post_id = $_GET[\'post\'];
    elseif( isset( $_POST[\'post_ID\'] ) ) $post_id = $_POST[\'post_ID\'];
    if( !isset( $post_id ) ) return;
 
    // Get the Page Template
    $template_file = get_post_meta( $post_id, \'_wp_page_template\', true );
    if($template_file == \'page-main-home.php\'){
     include \'/metaboxes/home-main-home.php\';
    }
}
而且似乎部分工作正常,因为我在管理部分的编辑页面上遇到了以下错误:

警告:include(/metaboxes/home main home.php)[函数.include]:打开流失败:没有这样的文件或目录

警告:include()[函数.include]:打开“/metaboxes/home main home”失败。php\'用于包含(include\\u path=\'。:/usr/local/php-5.3.15/share/pear\')

为什么include与simple一起工作:include \'metaboxes/home-main-home.php\'; ?

我还尝试了上面的代码,使用斜杠和不使用斜杠,这两种代码都会生成丢失/找不到文件的错误。

尝试三个:修复了我的模板输入错误,我不再使用以下内容访问文件:

add_action(\'admin_init\',\'load_home_meta\');
function load_home_meta() {
    // Get the Post ID
    if( isset( $_GET[\'post\'] ) ) $post_id = $_GET[\'post\'];
    elseif( isset( $_POST[\'post_ID\'] ) ) $post_id = $_POST[\'post_ID\'];
    if( !isset( $post_id ) ) return;
 
    // Get the Page Template
    $template_file = get_post_meta( $post_id, \'_wp_page_template\', true );
    if($template_file == \'page-main-home.php\'){
     include (TEMPLATEPATH . \'/metaboxes/home-meta.php\');
    }
}
但现在在每个元框中,我都会遇到以下错误:

警告:为/nfs/c07/h04/mnt/111667/domains/staging中的foreach()提供的参数无效。[站点名称]。org/html/wp-content/themes/[theme\\u-name]/metaboxes/home-meta。php在线184

但当我把这一点说出来,然后做一个简单的:

include \'metaboxes/home-meta.php\';
后端页面呈现良好。。。我不知道我做错了什么。

试试四个:所以,以上所有的方法仍然不起作用。

我再次尝试:

在管理后端的特定页面上隐藏编辑器,包括仅在主页(首页)使用元盒的自定义字段functions.php:

    /**
     * Hide Editor
     * @author Bill Erickson
     * @link http://www.billerickson.net/code/hide-editor-on-specific-page-template/
     */
    add_action( \'admin_init\', \'hide_editor\' );
    
    function hide_editor() {
        // Get the Post ID
        if( isset( $_GET[\'post\'] ) ) $post_id = $_GET[\'post\'];
        elseif( isset( $_POST[\'post_ID\'] ) ) $post_id = $_POST[\'post_ID\'];
        if( !isset( $post_id ) ) return;
     
        // Get the Page Template
        $template_file = get_post_meta( $post_id, \'_wp_page_template\', true );
        if($template_file == \'front-page.php\'){
          remove_post_type_support(\'page\', \'editor\');
           
            // add custom .js to footer for uploading images
                function custom_admin_js() {
                $url = get_option(\'siteurl\');
                $url = get_bloginfo(\'template_directory\') . \'/library/js/libs/meta-content.js\';
                echo \'"<script type="text/javascript" src="\'. $url . \'"></script>"\';
                }
                add_action(\'admin_footer\', \'custom_admin_js\');
                
    //          include \'metaboxes/home-meta.php\';
    
        }
    }
    
    // Loading Home Page Meta Box Code from External .php file
    add_action( \'add_meta_boxes_page\',\'load_home_meta\' );
    function load_home_meta() {
        global $post;
        if ( $post->ID == get_option( \'page_on_front\' ) ) {
            include( get_template_directory_uri() . \'/metaboxes/home-main-home.php\' );
        }
    }
我将以前的主模板文件更改为可识别的WordPressfront-page.php 因为我想把目标锁定在首页。

home-meta.php (位于“我的主题”文件夹的元数据库中)

    <?php
    
    /**
     * Home Page Custom Meta Content
     *
    **/
    
    function add_home_meta_box() {  
        add_meta_box(  
            \'home_meta_box\', // $id  
            \'Home Page Content\', // $title   
            \'show_home_meta_box\', // $callback  
            \'page\', // $page  
            \'normal\', // $context  
            \'high\'); // $priority
        add_meta_box(  
            \'home_meta_box_lower_1\', // $id  
            \'Home Lower Left\', // $title   
            \'show_home_meta_box_lower_left\', // $callback  
            \'page\', // $page  
            \'normal\', // $context  
            \'high\'); // $priority    
        add_meta_box(  
            \'home_meta_box_lower_2\', // $id  
            \'Home Lower Center\', // $title   
            \'show_home_meta_box_lower_center\', // $callback  
            \'page\', // $page  
            \'normal\', // $context  
            \'high\'); // $priority 
        add_meta_box(  
            \'home_meta_box_lower_3\', // $id  
            \'Home Lower Right\', // $title   
            \'show_home_meta_box_lower_right\', // $callback  
            \'page\', // $page  
            \'normal\', // $context  
            \'high\'); // $priority 
    }
    add_action(\'add_meta_boxes\', \'add_home_meta_box\'); 
    
    
    // Creating Array for Fields
    $prefix = \'home_\';
    $home_meta_fields = array(
        array(
            \'label\' => \'Caption Title\',
            \'desc\' => \'Upper section H2 caption title.\',
            \'id\' => $prefix.\'title\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Caption Sub Title\',
            \'desc\' => \'Upper section H3 caption title.\',
            \'id\' => $prefix.\'sub_title\',
            \'type\' => \'text\'
        ),
      array(  
          \'label\'=> \'Caption\',  
          \'desc\'  => \'Caption text block.\',  
          \'id\'    => $prefix.\'caption\',  
          \'type\'  => \'textarea\'  
      ),
        array(  
            \'label\'  => \'Caption Image\',  
            \'desc\'  => \'Upload a pre-cropped 1140px wide x 530px tall web-optimized image.\',  
            \'id\'    => $prefix.\'image\',  
            \'type\'  => \'image\'  
        )
    );// end caption array
    
    $prefix2 = \'home_lower_left_\';
    $home_meta_fields_lower_left = array(
        array(
            \'label\' => \'Column Title\',
            \'desc\' => \'H2 title for column.\',
            \'id\' => $prefix2.\'title\',
            \'type\' => \'text\'
        ),
        array(  
            \'label\'  => \'Column Image\',  
            \'desc\'  => \'Upload a pre-cropped 360px wide x 300px tall web-optimized image.\',  
            \'id\'    => $prefix2.\'image\',  
            \'type\'  => \'image\'  
        ),
        array(
            \'label\' => \'Column Sub-Title\',
            \'desc\' => \'H3 sub-title above text block.\',
            \'id\' => $prefix2.\'sub_title\',
            \'type\' => \'text\'
        ),
      array(  
          \'label\'=> \'Text block\',  
          \'desc\'  => \'Caption text block.\',  
          \'id\'    => $prefix2.\'caption\',  
          \'type\'  => \'textarea\'  
      ),
        array(
            \'label\' => \'Button Label\',
            \'desc\' => \'Button link label (what user reads on button).\',
            \'id\' => $prefix2.\'btn_label\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button Title Tag\',
            \'desc\' => \'On hover and SEO text.\',
            \'id\' => $prefix2.\'btn_title_tag\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button URL\',
            \'desc\' => \'URL where button links to. Enter http:// to work.\',
            \'id\' => $prefix2.\'btn_url\',
            \'type\' => \'text\'
        )
    );// end lower left array
    
    $prefix3 = \'home_lower_center_\';
    $home_meta_fields_lower_center = array(
        array(
            \'label\' => \'Column Title\',
            \'desc\' => \'H2 title for column.\',
            \'id\' => $prefix3.\'title\',
            \'type\' => \'text\'
        ),
        array(  
            \'label\'  => \'Column Image\',  
            \'desc\'  => \'Upload a pre-cropped 360px wide x 300px tall web-optimized image.\',  
            \'id\'    => $prefix3.\'image\',  
            \'type\'  => \'image\'  
        ),
        array(
            \'label\' => \'Column Sub-Title\',
            \'desc\' => \'H3 sub-title above text block.\',
            \'id\' => $prefix3.\'sub_title\',
            \'type\' => \'text\'
        ),
      array(  
          \'label\'=> \'Text block\',  
          \'desc\'  => \'Caption text block.\',  
          \'id\'    => $prefix3.\'caption\',  
          \'type\'  => \'textarea\'  
      ),
        array(
            \'label\' => \'Button Label\',
            \'desc\' => \'Button link label (what user reads on button).\',
            \'id\' => $prefix3.\'btn_label\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button Title Tag\',
            \'desc\' => \'On hover and SEO text.\',
            \'id\' => $prefix3.\'btn_title_tag\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button URL\',
            \'desc\' => \'URL where button links to. Enter http:// to work.\',
            \'id\' => $prefix3.\'btn_url\',
            \'type\' => \'text\'
        )
    );// end lower center array
    
    $prefix4 = \'home_lower_right_\';
    $home_meta_fields_lower_right = array(
        array(
            \'label\' => \'Column Title\',
            \'desc\' => \'H2 title for column.\',
            \'id\' => $prefix4.\'title\',
            \'type\' => \'text\'
        ),
        array(  
            \'label\'  => \'Column Image\',  
            \'desc\'  => \'Upload a pre-cropped 360px wide x 300px tall web-optimized image.\',  
            \'id\'    => $prefix4.\'image\',  
            \'type\'  => \'image\'  
        ),
        array(
            \'label\' => \'Column Sub-Title\',
            \'desc\' => \'H3 sub-title above text block.\',
            \'id\' => $prefix4.\'sub_title\',
            \'type\' => \'text\'
        ),
      array(  
          \'label\'=> \'Text block\',  
          \'desc\'  => \'Caption text block.\',  
          \'id\'    => $prefix4.\'caption\',  
          \'type\'  => \'textarea\'  
      ),
        array(
            \'label\' => \'Button Label\',
            \'desc\' => \'Button link label (what user reads on button).\',
            \'id\' => $prefix4.\'btn_label\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button Title Tag\',
            \'desc\' => \'On hover and SEO text.\',
            \'id\' => $prefix4.\'btn_title_tag\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button URL\',
            \'desc\' => \'URL where button links to. Enter http:// to work.\',
            \'id\' => $prefix4.\'btn_url\',
            \'type\' => \'text\'
        )
    );// end lower right array
    
    
    //The Callback
    function show_home_meta_box() {
    global $home_meta_fields, $post;
    // Using nonce for verification
    echo \'<input type="hidden" name="home_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
    
        //Begin field table and loop
        echo \'<table class="form-table">\';
        foreach ($home_meta_fields as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                            <td>\';
                            switch($field[\'type\']) {
                                // case items will go here
                                // text  
                                case \'text\':  
                                    echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" /> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                // textarea  
                                case \'textarea\':  
                                    echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                case \'image\':  
                                    $image = get_template_directory_uri().\'/library/images/img-preview-blank.png\';    
                                    echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }                 
                                    echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" /> 
                                                <img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';  
                                break;  
                            } // end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
    }
    
    // Save the Data
    function save_home_meta($post_id) {  
        global $home_meta_fields;  
          
        // verify nonce  
        if (!wp_verify_nonce($_POST[\'home_meta_box_nonce\'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if (\'page\' == $_POST[\'post_type\']) {  
            if (!current_user_can(\'edit_page\', $post_id))  
                return $post_id;  
            } elseif (!current_user_can(\'edit_post\', $post_id)) {  
                return $post_id;  
        }  
          
        // loop through fields and save the data  
        foreach ($home_meta_fields as $field) {  
            $old = get_post_meta($post_id, $field[\'id\'], true);  
            $new = $_POST[$field[\'id\']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field[\'id\'], $new);  
            } elseif (\'\' == $new && $old) {  
                delete_post_meta($post_id, $field[\'id\'], $old);  
            }  
        } // end foreach  
    }  
    add_action(\'save_post\', \'save_home_meta\');
    // end Upper Home Content
    
    // Lower Left
    //The Callback
    function show_home_meta_box_lower_left() {
    global $home_meta_fields_lower_left, $post;
    // Using nonce for verification
    echo \'<input type="hidden" name="home_meta_box_left_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
    
        //Begin field table and loop
        echo \'<table class="form-table">\';
        foreach ($home_meta_fields_lower_left as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                            <td>\';
                            switch($field[\'type\']) {
                                // case items will go here
                                // text  
                                case \'text\':  
                                    echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" /> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                // textarea  
                                case \'textarea\':  
                                    echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                case \'image\':  
                                    $image = get_template_directory_uri().\'/library/images/img-preview-blank.png\';    
                                    echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }                 
                                    echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" /> 
                                                <img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';  
                                break;  
                            } // end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
    }
    
    // Save the Data
    function save_home_meta_lower_left($post_id) {  
        global $home_meta_fields_lower_left;  
          
        // verify nonce  
        if (!wp_verify_nonce($_POST[\'home_meta_box_left_nonce\'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if (\'page\' == $_POST[\'post_type\']) {  
            if (!current_user_can(\'edit_page\', $post_id))  
                return $post_id;  
            } elseif (!current_user_can(\'edit_post\', $post_id)) {  
                return $post_id;  
        }  
          
        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_left as $field) {  
            $old = get_post_meta($post_id, $field[\'id\'], true);  
            $new = $_POST[$field[\'id\']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field[\'id\'], $new);  
            } elseif (\'\' == $new && $old) {  
                delete_post_meta($post_id, $field[\'id\'], $old);  
            }  
        } // end foreach  
    }  
    add_action(\'save_post\', \'save_home_meta_lower_left\');
    // end lower left
    
    // Lower Center Column
    //The Callback
    function show_home_meta_box_lower_center() {
    global $home_meta_fields_lower_center, $post;
    // Using nonce for verification
    echo \'<input type="hidden" name="home_meta_box_center_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
    
        //Begin field table and loop
        echo \'<table class="form-table">\';
        foreach ($home_meta_fields_lower_center as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                            <td>\';
                            switch($field[\'type\']) {
                                // case items will go here
                                // text  
                                case \'text\':  
                                    echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" /> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                // textarea  
                                case \'textarea\':  
                                    echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                case \'image\':  
                                    $image = get_template_directory_uri().\'/library/images/img-preview-blank.png\';    
                                    echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }                 
                                    echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" /> 
                                                <img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';  
                                break;  
                            } // end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
    }
    
    // Save the Data
    function save_home_meta_lower_center($post_id) {  
        global $home_meta_fields_lower_center;  
          
        // verify nonce  
        if (!wp_verify_nonce($_POST[\'home_meta_box_center_nonce\'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if (\'page\' == $_POST[\'post_type\']) {  
            if (!current_user_can(\'edit_page\', $post_id))  
                return $post_id;  
            } elseif (!current_user_can(\'edit_post\', $post_id)) {  
                return $post_id;  
        }  
          
        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_center as $field) {  
            $old = get_post_meta($post_id, $field[\'id\'], true);  
            $new = $_POST[$field[\'id\']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field[\'id\'], $new);  
            } elseif (\'\' == $new && $old) {  
                delete_post_meta($post_id, $field[\'id\'], $old);  
            }  
        } // end foreach  
    }  
    add_action(\'save_post\', \'save_home_meta_lower_center\');
    // end lower center
    
    // Lower Right Column
    //The Callback
    function show_home_meta_box_lower_right() {
    global $home_meta_fields_lower_right, $post;
    // Using nonce for verification
    echo \'<input type="hidden" name="home_meta_box_right_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
    
        //Begin field table and loop
        echo \'<table class="form-table">\';
        foreach ($home_meta_fields_lower_right as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                            <td>\';
                            switch($field[\'type\']) {
                                // case items will go here
                                // text  
                                case \'text\':  
                                    echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" /> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                // textarea  
                                case \'textarea\':  
                                    echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                case \'image\':  
                                    $image = get_template_directory_uri().\'/library/images/img-preview-blank.png\';    
                                    echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }                 
                                    echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" /> 
                                                <img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';  
                                break;  
                            } // end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
    }
    
    // Save the Data
    function save_home_meta_lower_right($post_id) {  
        global $home_meta_fields_lower_right;  
          
        // verify nonce  
        if (!wp_verify_nonce($_POST[\'home_meta_box_right_nonce\'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if (\'page\' == $_POST[\'post_type\']) {  
            if (!current_user_can(\'edit_page\', $post_id))  
                return $post_id;  
            } elseif (!current_user_can(\'edit_post\', $post_id)) {  
                return $post_id;  
        }  
          
        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_right as $field) {  
            $old = get_post_meta($post_id, $field[\'id\'], true);  
            $new = $_POST[$field[\'id\']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field[\'id\'], $new);  
            } elseif (\'\' == $new && $old) {  
                delete_post_meta($post_id, $field[\'id\'], $old);  
            }  
        } // end foreach  
    }  
    add_action(\'save_post\', \'save_home_meta_lower_right\');
    // end lower right
    
    ?>
当我保存所有这些内容时,在管理后端中会出现以下错误:

警告:include():http://wrapper在服务器配置中被/Users/[my\\u mac\\u user]/Sites/[dev\\u directory]/wp-content/themes/[site\\u-theme]/函数中的allow\\u url\\u include=0禁用。php在线354

警告:include(http://[site\\u url]/wp content/themes/[site\\u themes]/metaboxes/home main home.php):未能打开流:在/Users/[my\\u mac\\u user]/Sites/[dev\\u directory]/wp content/themes/[site\\u themes]/函数中找不到合适的包装器。php在线354

警告:include():无法打开“http://[站点url]/wp-content/themes/[站点主题]/metaboxes/home-main-home”。php(include\\u path=\'。:/Applications/MAMP/bin/php/php5.4.4/lib/php)包含在/Users/[my\\u mac\\u user]/Sites/[dev\\u directory]/wp content/themes/[site\\u theme]/函数中。php在线354

此外,我现在不再隐藏模板名称更改为的内容框front-page.php. 尝试使用$post的全局对象获取page_on_front 我也是,但这也不行。

错误:

注意:尝试在第328行的/Users/amores/Sites/[dev\\u目录]/wp-content/themes/[site\\u-theme/functions.php中获取非对象的属性

其他代码尝试失败:

    add_action( \'admin_init\', \'hide_editor\' );
    
    function hide_editor() {
        // Get the Post ID
        global $post;
        if ( $post->ID == get_option( \'page_on_front\' ) ) {
          remove_post_type_support(\'page\', \'editor\');
           
            // add custom .js to footer for uploading images
                function custom_admin_js() {
                $url = get_option(\'siteurl\');
                $url = get_bloginfo(\'template_directory\') . \'/library/js/libs/meta-content.js\';
                echo \'"<script type="text/javascript" src="\'. $url . \'"></script>"\';
                }
                add_action(\'admin_footer\', \'custom_admin_js\');
                
    //          include \'metaboxes/home-meta.php\';
    
        }
    }
谢谢!

2 个回复
SO网友:Chip Bennett

基于此评论:

@ChipBennett,这是网站的首页,也就是主页。

(旁注:在WordPress中,主页是not 这个Site Front Page, 而是Blog Posts Index. is_home() 在博客帖子索引上返回true,但不一定在网站首页上返回true。理解这一点很重要,因为它会使某些函数名和术语混淆。)

这其实很容易。如果您的site front page 是一个static page, 然后您可以找到该页面的ID,如下所示:

get_option( \'page_on_front\' );
下一步:将metabox代码挂钩到正确的挂钩中:\'add_meta_boxes\', 或者更准确地说:\'add_meta_boxes_page\':

add_action( \'add_meta_boxes_page\',\'load_home_meta\' );
function load_home_meta() {}
接下来,我将假设所有元框定义代码都在函数文件中/metaboxes/home-main-home.php? 我们只想有条件地加载它,具体操作如下:

global $post;
if ( $post->ID == get_option( \'page_on_front\' ) ) {
    // Load meta boxes file
}
接下来,您需要正确引用该文件。我假设我们正在处理一个主题?在这种情况下,您需要引用get_template_directory_uri():

include( get_template_directory() . \'/metaboxes/home-main-home.php\' );
总而言之:

add_action( \'add_meta_boxes_page\',\'load_home_meta\' );
function load_home_meta() {
    global $post;
    if ( $post->ID == get_option( \'page_on_front\' ) ) {
        include( get_template_directory() . \'/metaboxes/home-main-home.php\' );
    }
}
当然,这一切都假设/metaboxes/home-main-home.php 是正确的。。。

编辑使用get_template_directory() (文件路径)而不是get_template_directory_uri() 的(URL路径)include() 呼叫。

SO网友:PrivateUser

使用is_front_page()

所以它会像

if (is_front_page()) {
 include \'metaboxes/home-meta.php\';
}

结束