Forums

how do I do this with custom fields? (9 posts)

  1. erichmond
    Member
    Posted 2 years ago #

    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

  2. s_ha_dum (was apljdi)
    Member
    Posted 2 years ago #

    get_postmeta(). Just wrap it in an if statement.

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

  3. erichmond
    Member
    Posted 2 years ago #

    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

  4. vectyr
    Member
    Posted 2 years ago #

    <?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?

  5. alchymyth
    The Sweeper
    Posted 2 years ago #

    typo: try and use $mymeta between the divs

  6. vectyr
    Member
    Posted 2 years ago #

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

  7. vectyr
    Member
    Posted 2 years ago #

    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?

  8. alchymyth
    The Sweeper
    Posted 2 years ago #

    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?

  9. vectyr
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic