<?php $key=”mykey”; echo get_post_meta($post->ID, $key, true); ?>
this will output the value ONLY if the key “mykey” exist.
I sure wish I have known that myself when I built my template 🙂
Thanks for the tip, I had found this in the codex actually, I’ve been using it to set my background image:
<style>
body { background-image: url(<?php bloginfo('stylesheet_directory'); ?>/images/<?php echo get_post_meta($post->ID, "background", $single = true); ?>); background-repeat: repeat;}
</style>
This works fine.
But what I really need is a way to put the value for “background” into a variable so that I can evaluate it with if else type statements:
If: A custom field called “background” exists and contains a filename
Then: Set variable ‘$background_image’ to that filename
Else: Set variable ‘$background_image’ to a generic filename
…after that I can use $background_image as a variable in my style definition:
<style>
body { background-image: url(<?php bloginfo('stylesheet_directory'); ?>/images/<?php echo $background_image; ?>); background-repeat: repeat;}
</style>
That way I can specify a background image on a per-post basis, but if I don’t have one, a generic background will be used.
I understand the logic, it’s literally the correct PHP I’m having trouble with, can anyone help?
®
OK, I think I’ve got it:
<!– Decide on background graphic for the current page or post –>
<?php
// Capture the custom field “background” //
$background_graphic = get_post_meta($post->ID, “background”, $single = true);
// For the category Portfolio //
if (in_category(’12’)) {
// If there is no value use a generic one from Page 9 //
if ($background_graphic[0]==””) {
$background_graphic = get_post_meta(9, “background”, $single = true);
}
}
// For the category Play //
elseif (in_category(’25’)) {
// If there is no value use a generic one from Page 13 //
if ($background_graphic[0]==””) {
$background_graphic = get_post_meta(13, “background”, $single = true);
}
}
// For the category Blog //
elseif (in_category(’16’)) {
// If there is no value use a generic one from Page 12 //
if ($background_graphic[0]==””) {
$background_graphic = get_post_meta(12, “background”, $single = true);
}
}
// For the page Info and its children //
elseif (is_page(’11’) or $post->post_parent==”11″) {
// If there is no value use a generic one from Page 11 //
if ($background_graphic[0]==””) {
$background_graphic = get_post_meta(11, “background”, $single = true);
}
}
?>
<!– Body style to control page backgrounds using WP custom fields –>
<style>
body { background-image: url(<?php bloginfo(‘stylesheet_directory’); ?>/images/<?php echo $background_graphic; ?>); background-repeat: repeat;}
</style>
Seems to be working fine…
®
Ok 🙂 I am glad you made it work.
I just saw your response now, so I had no time to write another code ..
This routine is much more usefull than what people might think. it opens a whole different aspect for the wordpress-as-cms world…
I used a similar thing on my up-comming blog as well to seperate blog entries from portfolio-like image gallery entries.
if (post_custom('my_custom_field_name')) { ... }
is another way to check.
I did not know that second method.
I used the first method to “seperate” certain posts, that would appear on my blog, but not on the main page …(you can search it , you can link to it, but it actually shows no where …
Made value name “frontpage” and values “yes” , “no” or none.
Than on the front page template, filtered the results.
When it comes to layouts and graphic design, this method can really do a lot.