Support » Themes and Templates » Conditional tag if $customfield exists

  • Resolved mandawahoo

    (@mandawahoo)


    I am creating the page.php template for a new wordpress theme. When the custom field $post-image is included in the page, I want it to display with 2 columns where the $post-image is in the left column and the content is in the right column. When the $post-image does not display, I want the content to display in a div of the full width of the page.

    http://mandabee.com/wordpress/
    http://mandabee.com/wordpress/about

    See how on the index page the $post-image is included and the content displays how I’d like; however, on the about page there is no input for the $post-image, but yet the content is still displaying with the two columns. The code I have so far is:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <?php if ( $post-image ) { ?>

    <div class=”left”>

    <img src=”<?php echo get_post_meta($post->ID, ‘post-image’, true); ?>” alt=”Image for Page #<?php the_ID(); ?>” />

    </div><div class=”right”>

    <?php } else { ?>

    <div class=”post”><h2>“><?php the_title(); ?></h2>
    <?php } ?>

    <div class=”entry”>
    <?php the_content(); ?>
    </div>

    </div>
    <?php endwhile; else: ?>

    <p>Sorry, no posts matched your criteria.</p>

    <?php endif; ?>

    Thanks!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Almost right, but in the wrong way:

    In your case $post-image always will be undefined, since you didn’t query it yet.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <?php $image=get_post_meta($post->ID, 'post-image', true);
        <?php if ( $image ) { ?>
    
          <!-- layout with image -->
    
        <?php } else { ?>
    
          <!-- layout without image -->
    
        <?php endif; ?>
    
      <?php endwhile; else: ?>
        <p>Sorry, no posts matched your criteria.</p>
      <?php endif; ?>

    Peter

    Amazing, thank you! I’m learning as I go but it’s really fun. I appreciate the help.

    nbierma

    (@nbierma)

    Peter, when I try that block of code I get

    'Parse error: syntax error, unexpected '<' in [...]single.php on line 36'

    with line 36 being ‘ <?php if ( $image ) { ?>

    Any idea what I might be doing wrong?

    Thanks,

    Nathan

    huggie

    (@huggie)

    Nathan,

    You need to post the lines before it. It looks as though maybe you’ve already opened the PHP syntax.

    nbierma

    (@nbierma)

    Here’s what I’m working with:
    http://wordpress.pastebin.com/1rCXHWz0

    it’s loaded with custom fields; so far all of them are used on each post, I need to add one that only appears if it exists/has content.

    MichaelH

    (@michaelh)

    At a minimum, this line needs a closing PHP ?>

    <?php $image=get_post_meta($post->ID, 'post-image', true);

    so it would be

    <?php $image=get_post_meta($post->ID, 'post-image', true); ?>

    nbierma

    (@nbierma)

    I’ve actually gotten this to work using the other method I found elsewhere in the forums:


    <?php
    $key = 'image';
    $themeta = get_post_meta($post->ID, $key, TRUE);
    if($themeta != '') {
    echo '<img src='.$image.'>';
    }
    ?>

    If I had my preference I wouldn’t go this route because it requires echo-ing the HTML in quotes and stringing it to the variable. But that aside, does it make any difference which method I use?

    nbierma

    (@nbierma)

    OK let me try that, thanks.

    nbierma

    (@nbierma)

    Michael,

    Then I get a

    unexpected T_ENDIF in […]single.php on line 41

    huggie

    (@huggie)

    Michael’s suggestion was correct and I’ve edited the rest of the code here:

    http://wordpress.pastebin.com/64qCvfxW

    nbierma

    (@nbierma)

    That did it. Thanks!!

    huggie

    (@huggie)

    Glad that sorted it.

    Thanks for this!!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Conditional tag if $customfield exists’ is closed to new replies.