Kritical_V Posted November 6, 2011 Share Posted November 6, 2011 Hello. This is my first post here and I was hoping someone can give me a hand, I am trying to develop a small piece of code that will display and image on my blog however if it cannot be found it shows some text. <?php $filename = '<?php bloginfo('template_directory');?>/images/hubs/<?php single_cat_title();?>.jpg'; if (file_exists('bloginfo('$filename')) { //show it } else { echo "Hello World"; } dc ?> The problem is with the second line, I am using two wordpress functions to generate the URL however I am new to PHP and there is something wrong with my code. Can anyone help Please... Thank You Quote Link to comment Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 You are using PHP tags in your second line. You shouldn't be. It should be this: $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; Quote Link to comment Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 Double post Quote Link to comment Share on other sites More sharing options...
Kritical_V Posted November 6, 2011 Author Share Posted November 6, 2011 Parse error: syntax error, unexpected T_VARIABLE Thats on the line you just gave me any ideas whats wrong? <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists('bloginfo('$filename')) { //show it } else { echo "IMAGE NOT FOUND"; } dc ?> Quote Link to comment Share on other sites More sharing options...
Kritical_V Posted November 6, 2011 Author Share Posted November 6, 2011 Okay I fixed that problem, <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists($filename)) { //show it } else { } dc ?> However the following is not printing: . '/images/hubs/' . . '.jpg' again any ideas Thanks Quote Link to comment Share on other sites More sharing options...
Kritical_V Posted November 6, 2011 Author Share Posted November 6, 2011 Still need a fix if anyone can help Quote Link to comment Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 what does the output look like? Quote Link to comment Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 What's the issue now? What errors are you getting? The code looks fine, but I don't know what your functions are returning so I can't help. . '/images/hubs/' . . '.jpg' will never be output in your script. Quote Link to comment Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 It outputs the url wrong: http://www.mywebsite.com/content/themesEntertainment (Entertainment is the category name) It should print http://www.mywebsite.com/content/themes/images/hubs/Entertainment.jpg The /images/hubs/ & .jpg does not print. Quote Link to comment Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 Still not working, Anyone I really need help. Quote Link to comment Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 Okay been playing with it for the last hour and still nothing so I ask again for help. Please someone lend a hand. Quote Link to comment Share on other sites More sharing options...
haku Posted November 7, 2011 Share Posted November 7, 2011 You've got me. It's obviously returning values from the function calls, I can't imagine why it wouldn't be concatenating the text. Are you showing us your actual code there, or is it dummy code? I suppose you could break it down like this: $filename = bloginfo('template_directory'); $filename .= '/images/hubs/'; $filename .= single_cat_title(); $filename .= '.jpg'; But I really don't think this should be necessary. Quote Link to comment Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 No I have already tried that its still not outputting. Thats the actual code, The URL isn't tho. I'm considering rewarding whoever can do this... Quote Link to comment Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 We'd have to see your exact chunk of code. There's no output in the snippet you've provided. <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists($filename)) { echo 'Exists - '.$filename; } else { echo 'Not Exists - '.$filename; } ?> Quote Link to comment Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 Okay, <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists('bloginfo('$filename')) { //show it } else { echo "IMAGE NOT FOUND"; } dc ?> That's the exact code I'm using, It's outputting the following as text: http://www.mysitesadress.com/content/themes/mythemeEntertainment The http://www.mysitesadress.com/content/themes/mytheme is from the bloginfo('template_directory') bit and Entertainment is from the single_cat_title() bit It should out put the image found at: http://www.mysitesadress.com/content/themes/mytheme/images/hubs/Entertainment.jpg and if its not found should run the Else clause. Quote Link to comment Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 Have you tried running my code? The following works fine for me <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists($filename)) { echo 'Exists - '.$filename; } else { echo 'Not Exists - '.$filename; } function bloginfo($str) { return $str; } function single_cat_title() { return 'Entertainment'; } ?> Returns Not Exists - template_directory/images/hubs/Entertainment.jpg Quote Link to comment Share on other sites More sharing options...
haku Posted November 8, 2011 Share Posted November 8, 2011 I agree - the code you have been given and are showing will work fine. If it's not working, then you must have some other code that's doing something strange. I concatenate strings and function output all the time, and it works fine. For a test, just do this: <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; die($filename); ?> If it doesn't output the $filename correctly, I'm a monkey's uncle. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.