我试图在PDF存在的情况下将缩略图链接到相同名称的PDF,但如果PDF不存在则不链接到任何内容。这里是我的代码:无法让PHP file_exists正常工作
<?php
if (function_exists('has_post_thumbnail') && has_post_thumbnail()) {
$full_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full') ;
$pdf = substr_replace($full_image_url , 'pdf', strrpos($full_image_url[0] , '.') +1);
$filename = $pdf[0];
if (file_exists($filename)) {
echo '<a href="' . $pdf[0] . '" title="' . the_title_attribute('echo=0') . '" . target="_blank" >';
the_post_thumbnail('Full Size');
echo '</a>';
}
else {
echo "The file $filename exists";
}
}
?>
目前,else语句仅仅是为了证明它是否找到文件。它似乎是,因为它显示The file http://localhost/AWAD/wp-content/uploads/2012/03/+D.pdf exists
。如果我摆脱了条件,发布的缩略图会显示一个指向PDF的链接。我无法获得有条件工作。
任何人都可以发现它为什么不起作用吗?
'$ pdf [0]'的内容是什么? PDF与您的PHP脚本的位置有关的位置是什么? – Mischa