0

In wordpress there is a plugin that assigns a header graphic for each page. You call that header graphic by placing this code in your header.php file:

<?php if(function_exists('show_media_header')){ show_media_header();} ?>

This basically calls the image assigned and places it as an IMG in HTML. I would like to have it called as a background image with CSS but don't know how. For example:

.header-graphic{ background:url("show_media_header();"); }

I know that will obviously not work but that should explain what I'm trying to do.

Any help would be great. Thanks!

1

5 Answers 5

2

Depending on the scope of show_media_header() and that it actually returns the path to an image you could write the following:

.header-graphic{ background:url("<?php echo show_media_header(); ?>"); }

However this is of course under the assumption that your css is in the php-file, which wouldn't be recommended. You should look at using SASS or LESS instead.

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

2 Comments

Nop, that would result into .header-graphic{ background:url("<img src='something.png'>"); }
Thanks for the help! I tried it but nothing appears? This is what I have: <style type="text/css"> .header-graphic-background { background:url("<?php echo show_media_header(); ?>"); } </style>
2

It's generally a bad idea to serve static files (like CSS) dynamically, since it can't be cached effectively. So inserting the result of show_media_header() directly into your CSS is a no-go.

However, there is an alternative: Insert just that style into the HTML like so:

<h3 style='background-image: url("<?= show_media_header(); ?>");'>
   Foo
</h3>

Which can then be further modified by CSS that is in a statically-served and unchanging file - for example:

h3 {
   background-position: left 3px top 3px;
}

This of course assumes the function returns just the image URL; I've not used Wordpress personally.

Based on another comment, apparently this function generates a complete <img> tag (ugh!) so you might instead have to do something like this:

<h3>
   <?= show_media_header(); ?>
   Foo
</h3>

And style it as appropriate like so:

h3 img {
   margin: 3px 0 0 3px;
}

2 Comments

Ok so would I do something like this? <div style="background-image: url("<?= show_media_header();");"> My code blows up when I do that. Don't I need to close the php tag somewhere?
@crobley Yes, I made a typo
1

I'm gonna post it down here because no one is considering your statement:

"and places it as an IMG in HTML"

You may have to edit you plugin output. Since show_media_header(); echo a value, the function itself is creating a <img> element. Look for the plugin file, search for the function and, either create another one, duplicating the original, something like show_media_header_bg where you manipulate the element, or change the original.

2 Comments

Heres what I found: echo '<img src="'.$dhnd_image_url_base.$load_this_media.'" alt="'.$dhnd_alt_tag.'" title="'.$dhnd_alt_tag.'" />'; How can I change that to be a background image?
You can either change it for a echo "<div style='background-image: url(" . $dhnd_image_url_base . $load_this_media . ")' title='".$dhnd_alt_tag."'></div>" or you can replace that echo for return $dhnd_image_url_base . $load_this_media and instead of call show_header_image(); on header.php, you write a div or other element, with style="background-image:url(". show_header_image_bg() . ")"
0

How about if you use descendant CSS selectors as such:

#page #header {
   background-image: url("image.jpg");
}

#another-page #header {
   background-image: url("another-image.jpg");
}

and so assign each page to its background image.

Here, I'm assuming you can grab into each page by an id (here called "page" and "another-page", and that your header template has an id of header. It would help to see some HTML to see how best to exactly achieve this via CSS.

Comments

0

Got it to work!

Dug around in the plugin PHP file and found this:

function get_media_header_url() {
global $post;
$post_id = $post->ID;

So I did this:

.header-graphic-background{ background:url("images/<?php echo get_media_header_url() ?>"); }

Works great! You guys absolutely pointed me in the right direction. THANKS!!!

Comments

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.