WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditional tag if $customfield exists (14 posts)

  1. mandawahoo
    Member
    Posted 4 years ago #

    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!

  2. Peter Boosten
    Member
    Posted 4 years ago #

    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

  3. mandawahoo
    Member
    Posted 4 years ago #

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

  4. nbierma
    Member
    Posted 4 years ago #

    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

  5. huggie
    Member
    Posted 4 years ago #

    Nathan,

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

  6. nbierma
    Member
    Posted 4 years ago #

    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.

  7. MichaelH
    Member
    Posted 4 years ago #

    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); ?>
  8. nbierma
    Member
    Posted 4 years ago #

    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?

  9. nbierma
    Member
    Posted 4 years ago #

    OK let me try that, thanks.

  10. nbierma
    Member
    Posted 4 years ago #

    Michael,

    Then I get a

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

  11. huggie
    Member
    Posted 4 years ago #

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

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

  12. nbierma
    Member
    Posted 4 years ago #

    That did it. Thanks!!

  13. huggie
    Member
    Posted 4 years ago #

    Glad that sorted it.

  14. justbishop
    Member
    Posted 4 years ago #

    Thanks for this!!

Topic Closed

This topic has been closed to new replies.

About this Topic