WordPress.org

Ready to get started?Download WordPress

Forums

How can I call the featured image of each post to act as a background? (10 posts)

  1. EnmnM
    Member
    Posted 2 years ago #

    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.

  2. Andrew
    Forum Moderator
    Posted 2 years ago #

    [ 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?

  3. EnmnM
    Member
    Posted 2 years ago #

    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.

  4. Andrew
    Forum Moderator
    Posted 2 years ago #

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

  5. EnmnM
    Member
    Posted 2 years ago #

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

    How can I fix this?

  6. Andrew
    Forum Moderator
    Posted 2 years ago #

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

    What was supposed to be outputted?

  7. EnmnM
    Member
    Posted 2 years ago #

    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?

  8. Pioneer Valley Web Design
    Member
    Posted 2 years ago #

    Where did you write this?

    In a page template.

  9. EnmnM
    Member
    Posted 2 years ago #

    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.

  10. Pioneer Valley Web Design
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic