Support » Themes and Templates » How can I call the featured image of each post to act as a background?

  • I am using theme TwentyTen. I have identified one image in each post to be the featured image, which now appears on the main page with the post text around it.

    However, I want to use the image as a sprite, so I somehow need to identify the featured image as a background.

    First, I put this in the css: <?php header("Content-type: text/css"); ?>, immediately after my import files.

    Then, I wrote:

    <?php $attachment-post-thumbnail = get_image(); ?>
    a.rollover {display:block; width:300px; height:200px; text-decoration:none; background:url("<?php echo $attachment-post-thumbnail;?>");}
    a.rollover:hover {background-position:-300px 0;}

    ….and nothing happens. I’m not sure I copied all the relevant code of the sprite, but it works in html, so I am only concerned about the php part.

    Can someone help? Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    [ Moderator note: please use backtick or the code button for and code snippets. ]

    <?php $attachment-post-thumbnail = get_image(); ?>
    a.rollover {display:block; width:300px; height:200px; text-decoration:none; background:url("<?php echo $attachment-post-thumbnail;?>");}
    a.rollover:hover {background-position:-300px 0;}

    Where did you write this?

    I created a child of Twentyten. The code is under the import url in a section I called background elements. After that, I have sections that format the header, content, footer, etc. Everything else in my style sheet is working.

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    If you wrote it in a .css file, <?php $attachment-post-thumbnail = get_image(); ?> will not be recognised as CSS.

    Even with this preceding it?: <?php header(“Content-type: text/css”); ?>`

    How can I fix this?

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Any PHP code will not work, if the file extension is .css

    What was supposed to be outputted?

    So is there any way for me to call a variable in the css file so that I can set the variable for each post as a background?

    Where did you write this?

    In a page template.

    I tried putting it in the loop template but that didn’t work. Perhaps I didn’t write the code properly.

    I know html and some CSS, but php may as well be another language. And yes, I read the codex pages for loops and images.

    So is there any way for me to call a variable in the css file so that I can set the variable for each post as a background?

    Take a look at body_class

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How can I call the featured image of each post to act as a background?’ is closed to new replies.