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.

This is from the codex:

<?php if ( get_post_meta($post->ID, 'thumb', true) ) : ?>
    <a href="<?php the_permalink() ?>" rel="bookmark">
        <img class="thumb" src="<?php echo get_post_meta($post->ID, 'thumb', true) ?>" alt="<?php the_title(); ?>" />
    </a>
<?php endif; ?>

I want to include an if statement like the example above. But I can't figure out the proper way of doing that to the following:

<?php // Set and display custom field
        $mainbar_left_title = get_post_meta($post->ID, 'Mainbar Left Title', true);
        $mainbar_left_image = get_post_meta($post->ID, 'Mainbar Left Image', true); ?>
        <div class="float-left">
            <h2><?php echo $mainbar_left_title; ?></h2>
            <img src="<?php echo $mainbar_left_image ?>" alt="" />
        </div> <?php
    ?>
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

<?php if ( $mainbar_left_title && $mainbar_left_image ) : ?>

See http://www.php.net/manual/en/language.operators.logical.php

share|improve this answer
add comment

<?php if ( get_post_meta($post->ID, 'Mainbar Left Title', true) && get_post_meta($post->ID, 'Mainbar Left Image', true) ) ) : ?>
        <div class="float-left">
        <h2><?php echo $mainbar_left_title; ?>
            <img src="" alt="" />
        </div>
<?php endif; ?>

share|improve this answer
add comment

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.