WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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.