0

I've got a php variable, which i pass to a jquery script, which I want to set the background of a certain div. nothing seems to work though.

here's the code:

<?php $post_thumbnail_id = get_post_thumbnail_id(); ?>
<?php $post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id ); ?>
<script type="text/javascript">
  $(document).ready(function(){
      $("#event-sub-hero").css('background', 'url("<?php echo $post_thumbnail_url ?>")');
    });
</script>

<!-- SUBPAGE HEADER -->
  <div id="event-sub-hero">
    <div class="container">
      <div class="sub-hero-text col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3 col-xs-12">
        <h2><?php the_title(); ?></h2>
      </div> <!-- end sub-hero-text -->
    </div> <!-- end container -->
  </div> <!-- end sub-hero -->

<!-- END SUBPAGE HEADER -->

This is the code it spits out:

  $(document).ready(function(){
      $("#event-sub-hero").css('background', 'url("http://localhost:8888/babble/wp-content/uploads/2013/10/madmen-night.jpg")');
    });
4
  • 1
    Does the $post_thumbnail_url variable contain the full pathname to the image file i.e. images/pic1.gif Commented Oct 17, 2013 at 0:33
  • 1
    What is the generated code vs expected generated code? Commented Oct 17, 2013 at 0:34
  • What specifically is not working? Commented Oct 17, 2013 at 0:34
  • The images is not showing up. The variable does contain the full pathname too. Commented Oct 17, 2013 at 0:57

3 Answers 3

1

Try background-image instead of CSS background

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried just setting the background of that element with straight up CSS? Maybe the element isn't appearing how you would expect. Also, I'd use background-image as well.

Comments

0

Don't use the double quotes, you shouldn't need them, and as someone else mentioned, using 'background-image' is more correct when you are only setting the background image.

1 Comment

Removed the double quotes. still nothing .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.