Wordpress/PHP function and nested conditional

In Short: I am creating a WP function that will echo a image url based on the following conditions

in english here’s how the conditional should work

if custom taxonomy terms exist then build the img src url from custom taxonomy terms

elseif custom taxonomy does not exist - then display default image

else custom taxonomy exists but the image is not avail. in external url then display default image

here’s what I’ve got so far, I cant get step 3 to work

I did find this solution:
[php]

function is_200(){

$urlimage = ‘http://images.mysite.com/auto/mercedes-car.jpg’;
if(@getimagesize($urlimage) !== false)
{
echo “HAVE image”;
}
else
{
echo “Nop.”;
}
}[/php]

but I couldent get it to work with my function, below

[php]function cib_auto_thumbnail($width,$height) {
$timurl = “http://www.mywebsite.com/wp-content/plugins/timthumb.php?src=”;

$thumburl =“http://images.mywebsite.com/auto/”;
$defaultthumb =“http://images.mywebsite.com/auto/cib.jpg”;
$thumburl2 =".jpg";

$timmake =""; $i = 1; foreach((get_the_terms( $post->ID, ‘OriginalTag’ )) as $timmakecat) {if ($timmakecat->parent == 0) {$timmake .= $timmakecat->name; if($i == 1) break;}} ;
$timmodel =""; $i = 1; foreach((get_the_terms( $post->ID, ‘OriginalTag’ )) as $timmodelcat) {if ($timmodelcat->parent |= 0) {$timmodel .= $timmodelcat->name; if($i == 1) break;}} ;

if (empty($timmake) || empty($timmodel)) {

echo '<img src="' . $timurl . $defaultthumb .'" / >'; 

// if no make or model i.e. custom taxonomy is blank; then get default image 

// problem is a custom taxonomy can be created by the author - but the corrosponding image might not exist in the external file folder, hence I need the additional elseif to check for that

} else {

echo ‘<img src="’ . $timurl . $thumburl . $timmake . ‘-’ . $timmodel . $thumburl2 .’" / >’;

// else get image from make and model custom taxonomy and create image url 

} [/php]

**OriginalTags is the name of my custom taxonomy terms

*** Every image in the external location is named appropriately as: make-model.jpg i.e. $timmake-$timmodel.jpg

You stated your logic as:

if custom taxonomy terms exist then build the img src url from custom taxonomy terms
elseif custom taxonomy does not exist - then display default image

else custom taxonomy exists but the image is not avail. in external url then display default image</blockquote>

So, I see one issue. You do not want to waste time with extra compares. I mean, if you test a condition
such as “custom taxonomy does not exist”, then you already know the “custom taxonomy exists”. So, you
might want your logic to be more, well, more logical. First, you either display default image or you build a
special one. Only two output results. So, logically, it would be better in something like this format.

src=default image (Set up for just the default version, covers two results, no testing for those two.)

if custom taxonomy terms exist AND image IS avail. in external url
  then src = build the img src url from custom taxonomy terms

To explain, I listed your output results and came up with two. There were three conditions to test for.
Two of the conditions results in the default image. So, you only have to test for the one condition that
would create the non-default image. This is much better logic as it takes only two lines and one test.
In my example, I used src= where you would build the output. This is just an english example.

I think in this manner, it would be much faster and smoother and very little to test.

Hope this helps! (I didn’t create a version for your code. You would build the variable and then echo it.

Let us know…

Sponsor our Newsletter | Privacy Policy | Terms of Service