• Hello

    I’ve been trying to do something with custom fields but I’m not having any luck. How can I check for the existence of a particular custom field, and then take action depending on the result?

    For example:

    If: A custom field called “background” exists and has a value
    Then: Do something
    Else: Do something different

    Basically I’m using a custom field “background” on my pages and posts which holds an image file name. When that page or post is displayed, the image is used as the page background. Currently if no image is specified there is no background.

    The reason I want to do this is that if a post does not have an actual background image specified by the custom field, then I want to use a generic one.

    This seems like it ought to be straightforward but my grasp of PHP is not strong enough to quite manage it, any help would be gratefully received!

    ®

Viewing 8 replies - 1 through 8 (of 8 total)
  • <?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 🙂

    Thread Starter shirohagen

    (@shirohagen)

    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?

    ®

    Thread Starter shirohagen

    (@shirohagen)

    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.

    If anyone can help: http://wordpress.org/support/topic/177563?replies=4#post-838652

    Thanks

    PS: Sorry for posting this twice

    Michiel

    (@michiel)

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Check for the existence of a custom field’ is closed to new replies.