Powered By Blogger

Thursday, July 24, 2014

dynamically change background image in css with 2 ways


Using inline css:

<?php if (has_post_thumbnail() ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id(  ), 'single-post-thumbnail' ); ?>
<div class="blog_container_lb" style="background-image: url('<?php echo $image[0]; ?>')">
<?php endif; ?>

Using Jquery:

<?php 
if ( has_post_thumbnail() ) {
?>
<script type="text/javascript">
$('.blog_container_lb').css({'background-image': 'url('+<?php echo wp_get_attachment_image_src( get_post_thumbnail_id(  ), 'single-post-thumbnail' ); ?>+') no-repeat 0 0', 'min-height': '550px','margin': '0 auto','text-align': 'center'});

</script>
<?php
} ?>

No comments:

Post a Comment