Last active
August 29, 2015 14:04
-
-
Save donaldG/3ef3f8b24adca37e6f57 to your computer and use it in GitHub Desktop.
Add a fixed background image for a mobile device in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This will be in the functions file of your theme | |
*/ | |
// Add thumbnail support for site if it isn't there already | |
add_theme_support( 'post-thumbnails' ); | |
// Set thumbnail size and cropping, this is the mobile image size I'm using | |
add_image_size( 'mobile', 800, 800, true ); | |
// featured image source | |
function thumbnail_src( $thumb_name ) { | |
global $post; | |
if ( has_post_thumbnail() ) { | |
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $thumb_name ); | |
} | |
//You can add an else{} here if you like. Perhaps you have a placeholder image! | |
$thumb_src = $thumb['0']; | |
echo $thumb_src; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This will be in the header file of your theme | |
* According to our functions file above we can pass a parameter for the thumbnail image name. | |
* I've created a separate size for mobile devices | |
*/ | |
//giving the body a background image but saying if WordPress is mobile to create a div we can apply some styles too | |
<body <?php body_class(); ?> style="background-image:url('<?php if(! wp_is_mobile()){ thumbnail_src('full');} else{ }?>');"> | |
<?php if(wp_is_mobile()){?> | |
<div id="mobile_background" style="background-image:url('<?php thumbnail_src('mobile');?>');"></div> | |
<?php } ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Standard fixed background for body | |
* Mobile support for fixed backgrounds isn't great, hence why we're doing this | |
*/ | |
body{ | |
background:no-repeat top left; | |
background-size:cover; | |
-o-background-size:cover; | |
-moz-background-size:cover; | |
-webkit-background-size:cover; | |
background-attachment:fixed; | |
font-family:'adelle-1', 'Georgia', serif; | |
} | |
/** | |
* Mobile background div styles | |
* Mobile support for fixed positioned items ain't bad | |
*/ | |
#mobile_background{ | |
position:fixed; | |
top:0; | |
left:0; | |
z-index:-1; | |
width:100%; | |
height:100%; | |
background-attachment:scroll; | |
background-size:cover; | |
-o-background-size:cover; | |
-moz-background-size:cover; | |
-webkit-background-size:cover; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment