I det nyaste inbyggda WP-temat Twentyeleven finns det många trevliga funktioner man kan inkorporera i sina egna teman om man tycker om att skapa sådana. I denna post kommer jag gå igenom hur man skapar en egen ”Headers”-meny i sin Dashboard.
Det hela är faktiskt väldigt enkelt och kräver endast tillgång till filerna
- functions.php
- header.php
Börja med att öppna header.php och titta efter var du vill placera din banner. Klistra därefter in följande i din fil:
<?php
// Check if this is a post or page, if it has a thumbnail, and if it’s a big one
if ( is_singular() &&
has_post_thumbnail( $post->ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ’post-thumbnail’) ) &&
$image[1] >= HEADER_IMAGE_WIDTH ) :
// We have a new header image!
echo get_the_post_thumbnail( $post->ID, ’post-thumbnail’ );
else : ?>
<a href=”http://dinwebadress” ><img src=”<?php header_image(); ?>” width=”<?php echo HEADER_IMAGE_WIDTH; ?>” height=”<?php echo HEADER_IMAGE_HEIGHT; ?>” alt=”” /></a>
<?php endif; ?>
Om du inte vill att din banner ska vara klickbar så kan du ta bort de blå delarna. Glöm inte att byta ut adressen!
Sedan över till functions.php där du klistrar in följande:
<?php
/** Tell WordPress to run yourtheme_setup() when the ’after_setup_theme’ hook is run. */
add_action( ’after_setup_theme’, ’yourtheme_setup’ );if ( ! function_exists(’yourtheme_setup’) ):
/**
* @uses add_custom_image_header() To add support for a custom header.
* @uses register_default_headers() To register the default custom header images provided with the theme.
*
* @since 3.0.0
*/
function yourtheme_setup() {// This theme uses post thumbnails
add_theme_support( ’post-thumbnails’ );// Your changeable header business starts here
define( ’HEADER_TEXTCOLOR’, ” );
// No CSS, just IMG call. The %s is a placeholder for the theme template directory URI.
define( ’HEADER_IMAGE’, ’%s/images/headers/forestfloor.jpg’ );// The height and width of your custom header. You can hook into the theme’s own filters to change these values.
// Add a filter to yourtheme_header_image_width and yourtheme_header_image_height to change these values.
define( ’HEADER_IMAGE_WIDTH’, apply_filters( ’yourtheme_header_image_width’, 940 ) );
define( ’HEADER_IMAGE_HEIGHT’, apply_filters( ’yourtheme_header_image_height’, 198 ) );// We’ll be using post thumbnails for custom header images on posts and pages.
// We want them to be 940 pixels wide by 198 pixels tall (larger images will be auto-cropped to fit).
set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );// Don’t support text inside the header image.
define( ’NO_HEADER_TEXT’, true );// Add a way for the custom header to be styled in the admin panel that controls
// custom headers. See yourtheme_admin_header_style(), below.
add_custom_image_header( ”, ’yourtheme_admin_header_style’ );// … and thus ends the changeable header business.
// Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
register_default_headers( array (
’berries’ => array (
’url’ => ’%s/images/headers/berries.jpg’,
’thumbnail_url’ => ’%s/images/headers/berries-thumbnail.jpg’,
’description’ => __( ’Berries’, ’yourtheme’ )
),
’cherryblossom’ => array (
’url’ => ’%s/images/headers/cherryblossoms.jpg’,
’thumbnail_url’ => ’%s/images/headers/cherryblossoms-thumbnail.jpg’,
’description’ => __( ’Cherry Blossoms’, ’yourtheme’ )
),
’concave’ => array (
’url’ => ’%s/images/headers/concave.jpg’,
’thumbnail_url’ => ’%s/images/headers/concave-thumbnail.jpg’,
’description’ => __( ’Concave’, ’yourtheme’ )
),
’fern’ => array (
’url’ => ’%s/images/headers/fern.jpg’,
’thumbnail_url’ => ’%s/images/headers/fern-thumbnail.jpg’,
’description’ => __( ’Fern’, ’yourtheme’ )
),
’forestfloor’ => array (
’url’ => ’%s/images/headers/forestfloor.jpg’,
’thumbnail_url’ => ’%s/images/headers/forestfloor-thumbnail.jpg’,
’description’ => __( ’Forest Floor’, ’yourtheme’ )
),
’inkwell’ => array (
’url’ => ’%s/images/headers/inkwell.jpg’,
’thumbnail_url’ => ’%s/images/headers/inkwell-thumbnail.jpg’,
’description’ => __( ’Inkwell’, ’yourtheme’ )
),
’path’ => array (
’url’ => ’%s/images/headers/path.jpg’,
’thumbnail_url’ => ’%s/images/headers/path-thumbnail.jpg’,
’description’ => __( ’Path’, ’yourtheme’ )
),
’sunset’ => array (
’url’ => ’%s/images/headers/sunset.jpg’,
’thumbnail_url’ => ’%s/images/headers/sunset-thumbnail.jpg’,
’description’ => __( ’Sunset’, ’yourtheme’ )
)
) );
}
endif;if ( ! function_exists( ’yourtheme_admin_header_style’ ) ) :
/**
* Styles the header image displayed on the Appearance > Header admin panel.
*
* Referenced via add_custom_image_header() in yourtheme_setup().
*
* @since 3.0.0
*/
function yourtheme_admin_header_style() {
?>
<style type=”text/css”>
#headimg {
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
width: <?php echo HEADER_IMAGE_WIDTH; ?>px;
}
#headimg h1, #headimg #desc {
display: none;
}
</style>
<?php
}
endif;
?>
Ändra på sådant som är markerat i rött så att det passar ditt tema. Svårare är det faktiskt inte!