Forums

[resolved] Can I Use an If Statement If There Is No Custom Field Attached? (6 posts)

  1. andymacdonalduk
    Member
    Posted 2 years ago #

    Hey Guys,

    I currently use timthumb and custom fields to place images into my articles, which works completely fine unless i am doing a video post instead of an article.

    In a video post, i wouldn't insert an image, which is leaving the post with an 'image not found' icon. So my question is this. Is it possible to use an if statement based on whether a custom field is attached to a post or not.

    For example, if there is a custom field with the name 'thumbnail', display XXX output, if the post doesn't have a custom field with the name 'thumbnail', display AAA output.

    Any advice would be greatly appreciated.
    Andrew

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    You mean something like:

    <?php if( get_post_meta($post->ID, 'thumbnail', true) == '' ) {
    [ do XXX ]
    else [ do AAA ]
    ?>

    http://codex.wordpress.org/Function_Reference/get_post_meta

  3. andymacdonalduk
    Member
    Posted 2 years ago #

    Hi Esmi,

    Thank you for the reply. I have used the code that you have supplied, but im getting an error. This is how my code looks...

    <?php if( get_post_meta($post->ID, 'thumbnail', true) == '' ) {
    
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo get_post_meta($post->ID, "Thumbnail", $single = true); ?>&h=&w=165&zc=1&cropfrom=topleft" alt="<?php the_title(); ?>" class="postthumb" /></a>
    			<div class="wpn_post">
    				<?php the_content(); ?>
    			</div></div>
    	<div class="wpn_box-bottom"></div>	
    
    else <div class="wpn_post">
    				<?php the_content(); ?>
    			</div></div>
    	<div class="wpn_box-bottom"></div>
    ?>

    And this is the error it's producing in the browser...

    Parse error: syntax error, unexpected '<' in /home/celeb/public_html/wp-content/themes/Elements/single.php on line 27

    Any idea what i am doing wrong please?

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    You're missing a closing php ?>, your else needs something like } else {, and there's a missing closing } for that if statement:

    <?php if( get_post_meta($post->ID, 'thumbnail', true) == '' ) { ?>
    
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo get_post_meta($post->ID, "Thumbnail", $single = true); ?>&h=&w=165&zc=1&cropfrom=topleft" alt="<?php the_title(); ?>" class="postthumb" /></a>
    			<div class="wpn_post">
    				<?php the_content(); ?>
    			</div></div>
    	<div class="wpn_box-bottom"></div>	
    
    <?php } else { ?>
    <div class="wpn_post">
    				<?php the_content(); ?>
    			</div></div>
    	<div class="wpn_box-bottom"></div>
    <?php }
    ?>

    Related:
    http://php.net/manual/en/control-structures.elseif.php

  5. andymacdonalduk
    Member
    Posted 2 years ago #

    Thank you for such a quick response Michael,

    I have included the code you sent me in your quotes, and the error definitely is gone, however the post is still showing up with the original layout, even if there is no custom field attached.

    You can see the example here: http://celebritybackstage.com/taylor-swift-vs-beyonce-grammy-battle-who-will-win

    Im using this code...

    <?php if( get_post_meta($post->ID, 'Thumbnail', true) == '' ) { ?>
    
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo get_post_meta($post->ID, "Thumbnail", $single = true); ?>&h=&w=165&zc=1&cropfrom=topleft" alt="<?php the_title(); ?>" class="postthumb" /></a>
    			<div class="wpn_post">
    				<?php the_content(); ?>
    			</div></div>
    	<div class="wpn_box-bottom"></div>	
    
    <?php } else { ?>
    <div class="wpn_post">
    				<?php the_content(); ?>
    			</div></div>
    	<div class="wpn_box-bottom"></div>
    <?php }
    ?>
  6. andymacdonalduk
    Member
    Posted 2 years ago #

    Hey again,

    I have worked out my problem. I had the if statement the wrong way round, as in the two layouts. I swapped it around and i now have the result i was looking for.

    Thanks again for your support guys :-)

    Andrew

Topic Closed

This topic has been closed to new replies.

About this Topic