Archived topic
Dynamic background
7 replies · Started by Hans on March 7, 2019
Hi,
I followed your advice: Link. All trials to get an image with this code failed. I tried it with Elements and Hooks, nothing was successfull.
Where should I place the code?
In addition it is more complicate. What I want to achieve:
I have a page with a parameterized shortcode - see attached link. In dependence of the parameter for this shortcode I output different information from database tables. This part works very well. But in addition I would like to outut different backgrounds. Is it possible to realize this by PHP in combination with CSS or even with GP???
Hey Hans,
Where is the background image URL coming from? A custom field?
If so, you could:
1. Create a Hook Element: https://docs.generatepress.com/article/hooks-element-overview/
2. Set the hook to wp_head
3. Check "Execute PHP"
Then, inside the hook content, you can do this:
<?php
if ( is_singular() ) :
$url = get_post_meta( get_the_ID(), 'your_custom_field', true );
if ( $url ) : ?>
<style>
body {
background-image: url('<?php echo $url; ?>');
}
</style>
<?php endif;
endif;
?>
Let me know if that helps or not :)
Hi Tom,
I followed your advice. I created a Hook Element with Display Rule for the specific page. This is the code:
<?php
$url = 'http://huppenbroich.de/simwae/wp-content/wkfotos/wk-' . $_POST['stbez'] . '.jpg'; ?>
<style>
body {
background-image: url('<?php echo $url; ?>');
}
</style>
The image is loaded - but not displayed. There seems to be a problem in CSS.
Could you please investigate it.
Hi there,
the image URL is 404'ing - i tried opening it directly in the browser.
Sorry same issue - that page the button links to has a background image this is 404'ing.
Hi David,
thanks for your help and patience. There were two problems:
1. an erroe in the url-path
2. a corrupt jpg-file
No everything is working.
Glad to hear you figured it out :)