Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was wondering if it is possible to put in the code that you want something to display if there is content in the metabox.

I have a metabox in the post type projects, In this metabox, the customer can fill in the website of the project, this is showing in a div. If the customer doesn't fill in anything i want the div to dissapear.

It had to be something like isset i think.

What I had in mind

if (get_meta = true){
<div class="websitebutton"><?php meta('website-link-project'); ?></div>
}
else{
//nothing happens
}

But that's incorrect, just an idea of how i think it can be. I searched alot around on the internet, but can't find the answer.

I created the meta boxes with the morefields plugin.

Does someone has an idea of how i can get this work

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Under the premise, that website-link-project is the key of the postmeta in question and you know the post's ID (or this being in The Loop, respectively):

<?php
    /* store post ID and metadata in variables */
    $postid = get_the_ID();
    $website_link = get_post_meta( $postid, 'website-link-project', true );

    /* output some HTML on the condition of $website_link being meaningful */
    if ( ! empty( $website_link ) ) {
        echo '<div class="websitebutton">' . $website_link . '</div>';
    }
?>

The above might work as is, depending on whether my assumptions were correct.
Note that I don't know the plugin you are using at all.

get_the_ID will only work within The Loop, if you're somewhere else, you'll have to grab the ID from elsewhere as well.

get_post_meta is WP's native function for retrieval of post metadata. Your plugin might provide additional ones.

As for the conditional in the above:
PHP's empty pretty much does what its name tells you - it checks whether a variable is considered "empty" ('' a string with no characters, array() empty array, NULL, false, 0 integer zero, and such). Since we want the condition to be evaluated to true if the variable is not empty, we negate the expression with a !.

share|improve this answer
    
Thank you very much, that worked great! –  Maartje Jul 22 '13 at 12:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.