Wordpress Ninjas help please: Conditional IF statement using a custom field..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tonyparra
    Confirmed User
    • Jul 2008
    • 4568

    #1

    Wordpress Ninjas help please: Conditional IF statement using a custom field..

    Google was no help so again I turn to the community. I have some site that I used themes on that needed to have a custom field for the thumbnail (before featured images) like this :

    Code:
    <?php echo get_post_meta($post->ID, thumb, true); ?>
    Now that im adding content to these sites im using either a featured image or placeholder image. How can i correctly write the if statement to include the custom field first then if that isnt there the featured image and finally if that isnt there the placeholder image or code (i really want to use some ad script) .Someone please help with this I keep getting the white screen so i know im doing it wrong.

    High Performance Vps $10 Linode
    Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot
  • Brujah
    Beer Money Baron
    • Jan 2001
    • 22157

    #2
    The Codex link for get_post_meta, get_the_post_thumbnail:
    http://codex.wordpress.org/Function_.../get_post_meta
    http://codex.wordpress.org/Function_...post_thumbnail

    Something like this probably:
    Code:
    if ( $my_thumb = get_post_meta($post->ID, 'thumb', true) )
    {
    	// $my_thumb contains your value
    }
    elseif ( $my_thumb = get_the_post_thumbnail($post->ID, 'thumbnail') )
    {
    	// $my_thumb contains your value
    }
    else
    {
    	// display the placeholder image
    }

    Comment

    • Brujah
      Beer Money Baron
      • Jan 2001
      • 22157

      #3
      UNTESTED:

      Something like this might be easier to manage, add to your theme functions.php file:

      Code:
      function get_custom_thumb( $post_id ) {
      
      	// get_post_meta returns a string
      	if ( $my_thumb = get_post_meta($post_id, 'thumb', true) )
      	{
      		return '<img src="' . $my_thumb . '">';
      	}
      
      	// get_the_post_thumbnail returns the html img src
      	if ( $my_thumb = get_the_post_thumbnail($post_id, 'thumbnail') )
      	{
      		return $my_thumb;
      	}
      
      	// return your own html placeholder image
      	return '<img src="http://yoursite.com/image.jpg" />';
      }
      Then, in your template just use this where you want it displayed:
      Code:
      <?php echo get_custom_thumb( $post->ID ); ?>

      Comment

      • fris
        Too lazy to set a custom title
        • Aug 2002
        • 55679

        #4
        yeppers
        Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

        Comment

        • tonyparra
          Confirmed User
          • Jul 2008
          • 4568

          #5

          High Performance Vps $10 Linode
          Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot

          Comment

          • The Dawg
            Confirmed User
            • Apr 2002
            • 2438

            #6
            Nice, I could use this too.

            Thanks Brujah.

            Comment

            Working...