2016-03-04 75 views
2

如何將我的自定義元框添加到僅在管理頁面上的特定頁面?WordPress - 如何將自定義元框添加到特定的管理頁面?

這裏是我的自定義元框代碼,我是從here

/** 
* Adds a meta box to the post editing screen 
*/ 
function prfx_custom_meta() { 
    add_meta_box('prfx_meta', __('Meta Box Title', 'prfx-textdomain'), 'prfx_meta_callback', array('post', 'page')); 
} 

add_action('add_meta_boxes', 'prfx_custom_meta'); 


/** 
* Outputs the content of the meta box 
*/ 
function prfx_meta_callback($post) { 
    // echo 'This is a meta box'; 
    wp_nonce_field(basename(__FILE__), 'prfx_nonce'); 
    $prfx_stored_meta = get_post_meta($post->ID); 

    if ($post_slug == 'home') { 
    ?> 

    <p> 
     <label for="meta-text" class="prfx-row-title"><?php _e('Example Text Input', 'prfx-textdomain')?></label> 
     <input type="text" name="meta-text" id="meta-text" value="<?php if (isset ($prfx_stored_meta['meta-text'])) echo $prfx_stored_meta['meta-text'][0]; ?>" /> 
    </p> 

    <?php 
    } 
} 

/** 
* Saves the custom meta input 
*/ 
function prfx_meta_save($post_id) { 

    // Checks save status 
    $is_autosave = wp_is_post_autosave($post_id); 
    $is_revision = wp_is_post_revision($post_id); 
    $is_valid_nonce = (isset($_POST[ 'prfx_nonce' ]) && wp_verify_nonce($_POST[ 'prfx_nonce' ], basename(__FILE__))) ? 'true' : 'false'; 

    // Exits script depending on save status 
    if ($is_autosave || $is_revision || !$is_valid_nonce) { 
     return; 
    } 

    // Checks for input and sanitizes/saves if needed 
    if(isset($_POST[ 'meta-text' ])) { 
     update_post_meta($post_id, 'meta-text', sanitize_text_field($_POST[ 'meta-text' ])); 
    } 

} 
add_action('save_post', 'prfx_meta_save'); 

我只想元框添加到我的主頁。但現在在其他網頁和帖子我仍然看到元標題:

enter image description here

任何想法我如何阻止它展示在其他網頁和帖子?

編輯:

/** 
* Add custom meta box to a specific page in the WP admin. 
* 
* @ http://themefoundation.com/wordpress-meta-boxes-guide/ 
* @ http://www.farinspace.com/page-specific-wordpress-meta-box/ 
*/ 
function my_meta_init() { 
    // Get post/page ID. 
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ; 

    // Get post/page slug. 
    $post = get_post($post_id); 
    $slug = $post->post_name; 

    // checks for post/page slug. 
    if ($slug == 'home') { 
     add_meta_box('prfx_meta', __('Meta Box Title', 'prfx-textdomain'), 'prfx_meta_callback', array('post', 'page')); 
    } 
    add_action('add_meta_boxes', 'prfx_meta_save'); 
} 
add_action('admin_init','my_meta_init'); 

/** 
* Outputs the content of the meta box 
*/ 
function prfx_meta_callback($post) { 
    // echo 'This is a meta box'; 
    wp_nonce_field(basename(__FILE__), 'prfx_nonce'); 
    $prfx_stored_meta = get_post_meta($post->ID); 
    ?> 

    <p> 
     <label for="meta-text" class="prfx-row-title"><?php _e('Example Text Input', 'prfx-textdomain')?></label> 
     <input type="text" name="meta-text" id="meta-text" value="<?php if (isset ($prfx_stored_meta['meta-text'])) echo $prfx_stored_meta['meta-text'][0]; ?>" /> 
    </p> 

    <?php 
} 

/** 
* Saves the custom meta input 
*/ 
function prfx_meta_save($post_id) { 
    // Checks save status 
    $is_autosave = wp_is_post_autosave($post_id); 
    $is_revision = wp_is_post_revision($post_id); 
    $is_valid_nonce = (isset($_POST[ 'prfx_nonce' ]) && wp_verify_nonce($_POST[ 'prfx_nonce' ], basename(__FILE__))) ? 'true' : 'false'; 

    // Exits script depending on save status 
    if ($is_autosave || $is_revision || !$is_valid_nonce) { 
     return; 
    } 

    // Checks for input and sanitizes/saves if needed 
    if(isset($_POST[ 'meta-text' ])) { 
     update_post_meta($post_id, 'meta-text', sanitize_text_field($_POST[ 'meta-text' ])); 
    } 

} 
add_action('save_post', 'prfx_meta_save'); 
+0

http://www.farinspace.com添加以下代碼/ page-specific-wordpress-meta-box /你可以傳遞'$ _GET ['post']? $ _GET ['post']:$ _POST ['post_ID']'檢查您想要添加元框的頁面。 – Milap

回答

1

你可以試試這個條件

add_action('admin_init','my_meta_init'); 
function my_meta_init() 
{ 
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ; 
    // checks for post/page ID 
    if ($post_id == '84') 
    { 
    add_meta_box('my_all_meta_1', 'My Custom Meta Box 1', 'my_meta_setup_1', 'page', 'normal', 'high'); 
    } 
add_action('save_post','my_meta_save'); 
} 
+0

謝謝你的幫助! – laukok

+0

但我怎樣才能得到後slu而不是後ID? – laukok

+0

試試這個, $ post = get_post($ post_id); $ slug = $ post-> post_name; – meet

1
/** 
* Adds a meta box to the post editing screen 
*/ 
function prfx_custom_meta() { 
    $current_user = wp_get_current_user(); 
    if($current_user->roles[0] === 'administrator') { 
     add_meta_box('prfx_meta', __('Meta Box Title', 'prfx-textdomain'), 'prfx_meta_callback', array('post', 'page')); 
    } 
} 

add_action('add_meta_boxes', 'prfx_custom_meta'); 


/** 
* Outputs the content of the meta box 
*/ 
function prfx_meta_callback($post) { 
    // echo 'This is a meta box'; 
    wp_nonce_field(basename(__FILE__), 'prfx_nonce'); 
    $prfx_stored_meta = get_post_meta($post->ID); 

    if ($post_slug == 'home') { 
    ?> 

    <p> 
     <label for="meta-text" class="prfx-row-title"><?php _e('Example Text Input', 'prfx-textdomain')?></label> 
     <input type="text" name="meta-text" id="meta-text" value="<?php if (isset ($prfx_stored_meta['meta-text'])) echo $prfx_stored_meta['meta-text'][0]; ?>" /> 
    </p> 

    <?php 
    } 
} 

/** 
* Saves the custom meta input 
*/ 
function prfx_meta_save($post_id) { 

    // Checks save status 
    $is_autosave = wp_is_post_autosave($post_id); 
    $is_revision = wp_is_post_revision($post_id); 
    $is_valid_nonce = (isset($_POST[ 'prfx_nonce' ]) && wp_verify_nonce($_POST[ 'prfx_nonce' ], basename(__FILE__))) ? 'true' : 'false'; 

    // Exits script depending on save status 
    if ($is_autosave || $is_revision || !$is_valid_nonce) { 
     return; 
    } 

    // Checks for input and sanitizes/saves if needed 
    if(isset($_POST[ 'meta-text' ])) { 
     update_post_meta($post_id, 'meta-text', sanitize_text_field($_POST[ 'meta-text' ])); 
    } 

} 
add_action('save_post', 'prfx_meta_save'); 
+0

謝謝,但它仍然是我上面做的屏幕抓取相同... – laukok

+0

你想在合適的模板上使用它嗎? –

+0

頁面沒有模板 – laukok

0

使用這個js它能夠解決您的問題。這將顯示在perticular模板或頁面metabox ..

(function($){ 
    $(document).ready(function() { 

     var $page_template = $('#pageid') 
      ,$metabox1 = $('#metaboxid'); 

     $page_template.change(function() { 
      if ($(this).val() == 'templatename') { 
       $metabox1.show(); 

      } else { 
       $metabox1.hide(); 

      } 
     }).change(); 

    }); 
    })(jQuery); 

如果您在使用管理這個再使用管理鉤和functions.php中

相關問題