【发布时间】:2021-01-03 10:27:12
【问题描述】:
我用过一个函数
/*
Random File Function
Written By: Qassim Hassan
Website: wp-time.com
Twitter: @QQQHZ
*/
function Qassim_Random_File($folder_path = null){
if( !empty($folder_path) ){ // if the folder path is not empty
$files_array = scandir($folder_path);
$count = count($files_array);
if( $count > 2 ){ // if has files in the folder
$minus = $count - 1;
$random = rand(2, $minus);
$random_file = $files_array[$random]; // random file, result will be for example: image.png
$file_link = $folder_path . "/" . $random_file; // file link, result will be for example: your-folder-path/image.png
return '<a href="'.$file_link.'" target="_blank" title="'.$random_file.'"><img src="'.$file_link.'" alt="'.$random_file.'"></a>';
}
else{
return "The folder is empty!";
}
}
else{
return "Please enter folder path!";
}
}
?>
从特定的 wordpress 目录中提取随机图像。我可以成功调用一个随机图像文件,但它不显示,只显示图像文件名和损坏的图像图标。
我在教程中用来显示图像的代码是<?php echo Qassim_Random_File("my-folder-name"); // display random image! ?>
截至本周,我是 php 的新手,而且总体上是一个相当新手的编码器(知道一些基础知识),所以我真的很难过。我已经被堆栈溢出的其他可能解决方案愚弄了,但本教程是我唯一能够接近工作的东西。如果有人能发现问题,非常感谢!
这显示在 HTML 检查器中:
<div class="elementor-widget-container">
<h5>call</h5>
<a href="wp-content/uploads/H-PH-MA-Greyscale/image_2.jpg" target="_blank" title="image_2.jpg">
<img src="wp-content/uploads/H-PH-MA-Greyscale/image_2.jpg" alt="image_2.jpg">
</a>
</div>
【问题讨论】:
-
请edit您的问题包含生成链接的代码,因为这就是问题所在。所有相关代码都必须包含在问题本身中,因为外部链接可能会随着时间的推移而中断或更改,并使问题对未来遇到相同问题的用户没有帮助:)
-
谢谢抱歉!完成。
-
首先我们需要做一些调试,看看问题出在哪里,这样我们就可以找出需要修复的 what :) 让我们从 HTML 显示的内容开始检查损坏的图像时的元素检查器?这将告诉我们链接是否错误或图像标签格式错误。
-
是的!有效! :-) 和一个非常明确的答案。我非常感谢您的帮助。哦,是的,我不想在这里解决这些其他问题,只是想确认在这段代码的上下文中这样做是可能/有意义的。如果我遇到困难,我会尝试自己解决这个问题并发布一个新问题。只是想提高效率而不是追逐我的尾巴。再次感谢您的帮助!
-
只是想让您知道,通过反复试验,我能够拼凑出一些看起来完全符合我需要的东西! :-) 再次感谢!
标签: php wordpress image random