希望在有文件要下载时显示下载链接


want to display the download link whenever there is a file to download

我的问题是,就像标题所说的那样,我想仅在文件可用时才显示文件的下载链接......

我不知道错误在哪里:

<?php $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); ?>
<div id="custom_pdf">  
 <a href="<?php echo $doc['url']; ?> "> 
 Download PDF Here  
 </a> 
 </div><!-- #custom_pdf --> 

这是正常的代码..它的工作很好,但在这里它无条件地显示...有条件的守则是:

<?php $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); ?>
<? if(strlen(trim(<?php $doc['url'] ?>)) > 0) { 
<div id="custom_pdf">  
 <a href="<?php echo $doc['url']; ?> "> 
 Download PDF Here  
 </a> 
 </div><!-- #custom_pdf --> 

} ; ?> // end if  

这里有错误的地方,但我不知道在哪里。

有人可以帮我吗?谢谢。

您的 PHP 标签未正确放置在 HTML 代码中:

<?php $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);
if(strlen(trim($doc['url'])) > 0) {
?>
<div id="custom_pdf">  
<a href="<?php echo $doc['url']; ?>">Download PDF Here</a> 
</div><!-- #custom_pdf --> 
<?php } // end if  
?>
当你从 HTML 切换到 PHP 时,你需要

打开一个 PHP 标签<?php,当你从 PHP 切换到 HTML 时,你需要关闭 PHP 标签?>

当你

已经在php中时,你正在打开一个<?php标签

<?php 
  $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);
  if(strlen(trim($doc['url'])) > 0) 
  { 
?>
  <div id="custom_pdf">  
     <a href="<?php echo $doc['url']; ?> ">Download PDF</a> 
  </div><!-- #custom_pdf --> 
<?php 
  } 
?>

您可以尝试使用下载监视器插件。您可以设置包括图标在内的显示配置。它具有分析和文件交换功能。您还可以选中"强制下载"选项,以便文件不会尝试加载到新的浏览器窗口中。

http://wordpress.org/extend/plugins/download-monitor/