GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Wordpress Ninjas help please: Conditional IF statement using a custom field.. (https://gfy.com/showthread.php?t=1059466)

tonyparra 02-29-2012 12:29 PM

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.

Brujah 02-29-2012 12:38 PM

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
}


Brujah 02-29-2012 12:47 PM

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

fris 02-29-2012 01:41 PM

yeppers:pimp

tonyparra 03-01-2012 07:27 AM

:thumbsup:thumbsup

The Dawg 03-01-2012 08:27 AM

Nice, I could use this too.

Thanks Brujah. :thumbsup


All times are GMT -7. The time now is 01:57 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123