WordPress - 如何将自定义元框添加到特定的管理页面?
问题描述:
如何将我的自定义元框添加到仅在管理页面上的特定页面?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');
我只想元框添加到我的主页。但现在在其他网页和帖子我仍然看到元标题:
任何想法我如何阻止它展示在其他网页和帖子?
编辑:
/**
* 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');
答
你可以试试这个条件
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');
}
答
/**
* 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');
答
使用这个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中
http://www.farinspace.com添加以下代码/ page-specific-wordpress-meta-box /你可以传递'$ _GET ['post']? $ _GET ['post']:$ _POST ['post_ID']'检查您想要添加元框的页面。 – Milap