不同页面上不同的自定义页眉图像

时间:2014-12-10 作者:Jornes

我现在正在为我的客户创建一个网站,该客户希望我使标题图像部分的每一页都能够由他们在wordpress管理面板中轻松上传。

这是我在函数中的代码。php:

add_theme_support( \'custom-header\' );
$args = array(
    \'width\'         => 950,
    \'height\'        => 150,
    \'default-image\' => get_template_directory_uri() . \'/images/header.jpg\',
    \'uploads\'       => true,
);
add_theme_support( \'custom-header\', $args );
此代码用于单个横幅图像。如果我想让它可用于不同的页面和其他页面,该怎么办?

2 个回复
SO网友:Manyang

在你的主题中创建一个名为header custom的新文件。phpnext将此代码复制到该文件中

    class customheaderMetabox {
    private $screen = array(
        \'post\',
        \'page\',
    );
    private $meta_fields = array(
        array(
            \'label\' => \'Add Image Header\',
            \'id\' => \'addimageheader_24744\',
            \'type\' => \'media\',
        ),
    );
    public function __construct() {
        add_action( \'add_meta_boxes\', array( $this, \'add_meta_boxes\' ) );
        add_action( \'admin_footer\', array( $this, \'media_fields\' ) );
        add_action( \'save_post\', array( $this, \'save_fields\' ) );
    }
    public function add_meta_boxes() {
        foreach ( $this->screen as $single_screen ) {
            add_meta_box(
                \'customheader\',
                __( \'Custom Header\', \'textdomain\' ),
                array( $this, \'meta_box_callback\' ),
                $single_screen,
                \'advanced\',
                \'default\'
            );
        }
    }
    public function meta_box_callback( $post ) {
        wp_nonce_field( \'customheader_data\', \'customheader_nonce\' );
        echo \'Allow you to add custom header in every page\';
        $this->field_generator( $post );
    }
    public function media_fields() {
        ?><script>
            jQuery(document).ready(function($){
                if ( typeof wp.media !== \'undefined\' ) {
                    var _custom_media = true,
                    _orig_send_attachment = wp.media.editor.send.attachment;
                    $(\'.customheader-media\').click(function(e) {
                        var send_attachment_bkp = wp.media.editor.send.attachment;
                        var button = $(this);
                        var id = button.attr(\'id\').replace(\'_button\', \'\');
                        _custom_media = true;
                            wp.media.editor.send.attachment = function(props, attachment){
                            if ( _custom_media ) {
                                $(\'input#\'+id).val(attachment.url);
                            } else {
                                return _orig_send_attachment.apply( this, [props, attachment] );
                            };
                        }
                        wp.media.editor.open(button);
                        return false;
                    });
                    $(\'.add_media\').on(\'click\', function(){
                        _custom_media = false;
                    });
                }
            });
        </script><?php
    }
    public function field_generator( $post ) {
        $output = \'\';
        foreach ( $this->meta_fields as $meta_field ) {
            $label = \'<label for="\' . $meta_field[\'id\'] . \'">\' . $meta_field[\'label\'] . \'</label>\';
            $meta_value = get_post_meta( $post->ID, $meta_field[\'id\'], true );
            if ( empty( $meta_value ) ) {
                $meta_value = $meta_field[\'default\']; }
            switch ( $meta_field[\'type\'] ) {
                case \'media\':
                    $input = sprintf(
                        \'<input style="width: 80%%" id="%s" name="%s" type="text" value="%s"> <input style="width: 19%%" class="button customheader-media" id="%s_button" name="%s_button" type="button" value="Upload" />\',
                        $meta_field[\'id\'],
                        $meta_field[\'id\'],
                        $meta_value,
                        $meta_field[\'id\'],
                        $meta_field[\'id\']
                    );
                    break;
                default:
                    $input = sprintf(
                        \'<input %s id="%s" name="%s" type="%s" value="%s">\',
                        $meta_field[\'type\'] !== \'color\' ? \'style="width: 100%"\' : \'\',
                        $meta_field[\'id\'],
                        $meta_field[\'id\'],
                        $meta_field[\'type\'],
                        $meta_value
                    );
            }
            $output .= $this->format_rows( $label, $input );
        }
        echo \'<table class="form-table"><tbody>\' . $output . \'</tbody></table>\';
    }
    public function format_rows( $label, $input ) {
        return \'<tr><th>\'.$label.\'</th><td>\'.$input.\'</td></tr>\';
    }
    public function save_fields( $post_id ) {
        if ( ! isset( $_POST[\'customheader_nonce\'] ) )
            return $post_id;
        $nonce = $_POST[\'customheader_nonce\'];
        if ( !wp_verify_nonce( $nonce, \'customheader_data\' ) )
            return $post_id;
        if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
            return $post_id;
        foreach ( $this->meta_fields as $meta_field ) {
            if ( isset( $_POST[ $meta_field[\'id\'] ] ) ) {
                switch ( $meta_field[\'type\'] ) {
                    case \'email\':
                        $_POST[ $meta_field[\'id\'] ] = sanitize_email( $_POST[ $meta_field[\'id\'] ] );
                        break;
                    case \'text\':
                        $_POST[ $meta_field[\'id\'] ] = sanitize_text_field( $_POST[ $meta_field[\'id\'] ] );
                        break;
                }
                update_post_meta( $post_id, $meta_field[\'id\'], $_POST[ $meta_field[\'id\'] ] );
            } else if ( $meta_field[\'type\'] === \'checkbox\' ) {
                update_post_meta( $post_id, $meta_field[\'id\'], \'0\' );
            }
        }
    }
}
if (class_exists(\'customheaderMetabox\')) {
    new customheaderMetabox;
};
接下来在函数中包含文件。PHP使用此代码include(get_template_directory().\'/header-custom.php\');

完成此步骤后,您将在帖子和wp admin页面中看到新的metabox。

接下来,如果您想显示该图像,请将此代码放在标题中。php或在页面中。php<img src="<?php echo get_post_meta( $post->ID, \'addimageheader_24744\' , true);?>">

SO网友:Amit Mishra

我正在使用此解决方案

我创建了一个名为banner的自定义字段,并将其放置在标题中。php文件或标题单一。php文件。

<?php if(get_post_meta(get_the_ID(), \'banner\', true)) : ?>
<img src="<?php echo get_post_meta(get_the_ID(), \'banner\', true); ?>" />
<?php else : ?>
<?php the_title(); ?>
<?php endif; ?>
我觉得很好用

结束

相关推荐

Headers already sent by

Warning: Cannot modify header information - headers already sent by (output started at /forums/vb/search/searchcontroller/version.php:91) in /wp-includes/functions.php on line 862 我不知道这个错误意味着什么。有人能帮帮我吗?函数第862行。php是。。。setcookie( \'wp-settings-time-\'