Context: For a site’s images used in a particular section, each post relies on a base name put into meta data, which then is used with automatic extensions to generate large images, gallery thumbnail images, and an index page thumbnail as well.
For example, if Example_Name is the base name, then:
Example_Name_2-LG.jpg
is the second large image in the series
Example_Name_2_SM.jpg
is the corresponding second gallery thumbnail image
Example_Name_IN.jpg
is the index thumbnail chosen to represent the set
By using meta data and PHP conditionals, all the client has to do is put in the base name once and upload the appropriately named images to the Uploads folder, and the page template fills in the blanks.
All this is working fine, with one catch. There are seven slots for the thumbnails, and the page displays all of the thumbnail divs, even if there are less than seven thumbnail images in the Uploads folder.
I’d like to wrap each thumbnail div with a conditional that uses file_exists
to check for the existence of thumbnail images actually in the Uploads folder, so that if the named image doesn’t exist at the indicated file path, the corresponding empty div (and its hyperlink) won’t appear.
I’ve tried constructing absolute paths using the wp_uploads_dir
function, as well as bloginfo('template_directory')
and even the deprecated TEMPLATEPATH
, but have only succeeded in generating PHP errors. I’m assuming this is a path problem or something particular I don’t understand about the PHP function file_exists
.
Page blow-up example using wp_upload_dir:
<?php
$upload_dir = wp_upload_dir();
if ( file_exists( echo $upload_dir['baseurl'] . '/' . echo get_post_meta($post->ID, '_meta_example_name', true) . '_7_SM.jpg') ) {
?>
<div id="thumb7" class="thumb"> <!-- Should appear only when Example_Name_7_SM.jpg exists -->
...
</div>
<?php } ?>
Any suggestions appreciated.
You can’t use a file url in
file_exists()
like this:You should rather use an absolute file path, for example:
Then you should try
where
or
In this line…
… you don’t want all those
echo
s. You aren’t trying toecho
anything. You’ve even gotecho
s interspersed with string concatenation. Leave all those out.I would probably check for the post meta earlier though and skip the call to
file_exists
if the meta key is empty.You should use
__DIR__
. So assuming you’re checking inside thefunctions.php
file, it would be:if you need for theme you can use my function
use