Crap, I was missing a ) in front of the image url.
Well, I hope this method helps someone!
Well, since we’re at it, can someone help me add an if else statement to display a default image in case I don’t define the custom fields?
Thanks!
Well, I searched a bit and got that too, just added this:
<?php
$values = get_post_custom_values(“background-image”); // set the img custom name
if (isset($values[0])) { // if there’s a img in my custom field
?>
<style type=”text/css”> body { background-image: url(<?php bloginfo(‘template_url’); ?><?php echo get_post_meta($post->ID, ‘background-image’, true); ?>); background-color: <?php echo get_post_meta($post->ID, ‘background-color’, true); ?>; } </style>
<?php } // end if statement
// if there’s no img do replace it with no-img
else { ?>
<style type=”text/css”> body { background-image: url(<?php bloginfo(‘template_url’); ?>/images/ground01.png);
background-color:#aa4339; } </style>
<?php } ?>
Based it on:
this post
Actually this last code doesn’t work, it always displays the default image. What am I doing wrong?
Fixed:
<?php
$number = get_post_meta($post->ID, ‘background-image’, true);
if ( $number ) {
?>
<style type=”text/css”> body { background-image: url(<?php bloginfo(‘template_url’); ?><?php echo get_post_meta($post->ID, ‘background-image’, true); ?>); background-color: <?php echo get_post_meta($post->ID, ‘background-color’, true); ?>; } </style>
<?php } // end if statement
// if there’s no img do replace it with no-img
else { ?>
<style type=”text/css”> body { background-image: url(<?php bloginfo(‘template_url’); ?>/images/ground01.png);
background-color:#aa4339; } </style>
<?php } ?>
Thnx to this guy.
JL
(@johnlunceford)
Hey Mac, thanks for sharing this. I’m not much of a coder but I’m doing my best to learn in order to get the visual details i want.
Please confirm this…
on the page.php
below <?php get_header(); ?>
put:
<style type=”text/css”>
body {
background-image: url(<?php bloginfo(‘template_url’); ?><?php echo get_post_meta($post->ID, ‘background-image’, true); ?>;
background-color: <?php echo get_post_meta($post->ID, ‘background-color’, true); ?>;
}
</style>
When I examine the source code it successfully returns:
<style type=”text/css”>
body {
background-image: url(http://localhost/fragmentos/wp-content/themes/fragmentos/images/ground01.png;
background-color: #aa4339;
}
</style>
define the custom fields in the individual pages
example:
background-color -> #aa4339
background-image -> /images/ground01.png
Could you just confirm where the last piece of PHP is located?
Thanks!
John