首页 文章

WordPress - 如何将自定义元框添加到特定管理页面?

提问于
浏览
2

如何仅在管理页面上将自定义元框添加到 specific 页面?

这是我从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' );

我只想将元框添加到我的 home page . 但现在在其他页面和帖子上我仍然看到元 Headers :

enter image description here

知道如何阻止它在其他页面和帖子上显示吗?

EDIT:

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

3 回答

  • 0

    你可以试试这个条件

    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');
    }
    
  • 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' );
    
  • 1

    使用此js它将解决您的问题 . 这将在特定模板或页面上显示元数据 .

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

    如果你在管理员中使用它,那么使用admin hook并在functions.php中添加此代码

相关问题