chaseman Posted March 25, 2011 Share Posted March 25, 2011 I'm working on a WordPress theme and I'm trying to build in a simple if statement which will check if the user has add his own logo into the images folder if he doesn't then the name of the blog will appear as normal text in place of the graphic logo. This is how it looks like: <?php $logo_dir = get_template_directory_uri() . "/images/logo.png"; if (file_exists($logo_dir)) { ?> <li><img src="<?php bloginfo ('template_directory'); ?>/images/logo.png" alt="<?php bloginfo('description'); ?>" /></li> <?php } else { ?> <li id='blog_name'><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?><font>*</font></a></li> <?php } ?> I've echo'd out $logo_dir, the URL is correct, but still for some reason it's seeing it as if there would no file exist. If I turn it around into !file_exists then the graphic logo WILL show up. So it's always seeing it as non-existent. Any ideas why this could be or am I using this function in a wrong fashion? As far as I've understood the PHP manual, this is the correct way of using it. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 25, 2011 Share Posted March 25, 2011 file_exists() does NOT work with the http:// protocol. Why wouldn't you just use a local file system path to do this? Quote Link to comment Share on other sites More sharing options...
DavidAM Posted March 25, 2011 Share Posted March 25, 2011 ^^What he said ... and you are not using the same function call to get the name to test as you are to get the name to display $logo_dir = get_template_directory_uri() . "/images/logo.png"; if (file_exists($logo_dir)) { ?> <li><img src="<?php bloginfo ('template_directory'); ?>/images/logo.png" alt="<?php bloginfo('description'); ?>" /></li> I don't know the WordPress API, but I would guess that get_template_directory_uri() is going to return a value suitable for sending to the browser; and that bloginfo('template_directory') is going to return a file path suitable for use in PHP. Check for the proper usage of those functions. Quote Link to comment Share on other sites More sharing options...
chaseman Posted March 25, 2011 Author Share Posted March 25, 2011 Thanks for the responses. To David, bloginfo will return the full http URL as well, on top of it will even echo it out, which won't enable me to put it into a variable since then I'd get the URL displayed on the page, that's why I used the other function which doesn't have an echo. I think I could just add the relative URL myself e.g. ('images/logo.png'), I don't how much problems it could cause when other user's are using the template? I'll see. EDIT: I just tried adding the relative URL by hand like this: $logo_dir = "/images/logo.png"; But this does not work either. When I put this php file into my themes folder it will work then: <?php $logo = "images/logo.png"; if (file_exists($logo)) { echo "works"; } ?> For some reason it just doesn't work inside the Wordpress header.php file, it could be a wordpress specific thing. They could have disabled or modified it or something like that. Quote Link to comment Share on other sites More sharing options...
chaseman Posted March 25, 2011 Author Share Posted March 25, 2011 Ok changing the path to this: $logo = "wp-content/themes/chase_theme/images/logo.png"; Made it work, thanks for all the help. 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.