• If I have a custom field with a value how can I display it and some other html on a page – like contained in a div etc, if the custom field is empty I don’t want it to display it or the other html on the page

Viewing 8 replies - 1 through 8 (of 8 total)
  • get_postmeta(). Just wrap it in an if statement.

    $mymeta = get_post_meta($post_id, ‘mymeta’, true);
    if ($mymeta) {
    echo ‘<div>’.$mymeta.'</div>’;
    }

    Thread Starter Elliott Richmond

    (@erichmond)

    hey thanks for this, much appreciated.

    I couldn’t get it to work for a while but I changed the $post_id to $post->ID then it worked

    <?php $mymeta = get_post_meta($post->ID, 'mymeta', true);
    if ($mymeta) {
    echo '<div class="sideBox">'.mymeta.'</div>';
    } ?>

    I’m using this which seems to be the correct way to set it up according to this thread, but it is not wrapping the value with the div. Any ideas?

    typo: try and use $mymeta between the divs

    Ah, whoops. Hmm. I changed it and it’s still not working.

    Ok, I know that I am doing something wrong. I was putting the code inside my subpage template, but then I realized that this isn’t what I was wanting.

    I’m basically wanting some code to recognize a custom field and then wrap the value in a div wherever it occurs in the post. Would I put this in my functions.php?

    is your custom field really called ‘mymeta’ ?
    any data in the custom field?
    what does the html in the browser show?
    what is the surrounding code like?
    is it in the loop?

    Ok, I figured out my error here. The code works fine if I place it in the loop. I completely forgot that I was using a plugin called “EmbedIt” which allows you to drop the content of a custom field anywhere within the content of the editor using a shortcode like [HTML1].

    This is where the problem was. The custom field key is HTML1, so I don’t think anyone will be able to help me unless they are familiar with this plugin. What I am basically trying to accomplish, is allowing a client to insert blocks of text via custom fields that they can then insert into the body copy of their text. I don’t want them to have to mess with divs, I’d like to have the divs inserted automatically.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘how do I do this with custom fields?’ is closed to new replies.