我有一个函数来显示给定目录中的文件列表作为链接。然后我有一个短代码来显示函数,但我无法弄清楚如何在不使用内联php调用函数的情况下使短代码工作。短代码现在只能工作,因为我有另一个第三方插件允许在WordPress文本编辑器中使用内联php。
这是功能:
function showFiles($path){
if ($handle = opendir($path))
{
$up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)) {
echo "<li><a href='/".$file."' target=_blank>".$fName."</a></li>";
}
}
}
closedir($handle);
}
}
这是我正在使用的短代码功能的简化版本:
function sc_showfiles( $atts, $content = null ) {
extract( shortcode_atts( array(
'type' => '',
), $atts ) );
$showfiles = '<ul><?php showFiles(\'files/'.$type.'files/'.do_shortcode($content).'\'); ?></ul>';
return $showfiles;
}
add_shortcode('showfiles', 'sc_showfiles');
内容区域允许用户输入带有用户生成元数据的短代码,因此它所从的目录将与登录用户匹配。
我已经尝试了六种不同的东西,并且如果不使用内联php调用showFiles函数就无法实现这一点。我使用了一次接近:
$files = showFiles('files/'.$type.'files/'.do_shortcode($content).);
和
$showfiles = '<ul>'.$files.'</ul>';
return $showfiles;
但是把文件列表放在页面顶部,我假设因为原始的showFiles函数正在回显html输出。但如果我改变回声以返回顶部函数,我什么也得不到。
最后的工作代码//////////////////////// ///感谢FaddishWorm的帮助。
function showFiles($path){
if ($handle = opendir($path))
{
$up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
$thefiles .= '<span class="checkmarks"><ul>';
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file !== "index.html")
{
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)) {
$thefiles .= "<li><a href='/".$file."' target=_blank>".$fName."</a></li>";
}
}
}
closedir($handle);
$thefiles .= '</ul></span>';
return $thefiles;
}
}
function sc_showfiles( $atts, $content = null ) {
extract( shortcode_atts( array(
'type' => '',
), $atts ) );
$thefiles = showFiles('files/'.$type.'files/'.do_shortcode($content));
return $thefiles;
}
add_shortcode('showfiles', 'sc_showfiles');
答案 0 :(得分:1)
从函数中返回内容不会将其回显到页面。当您致电您的功能时,您可以尝试echo showFile(blah);
如果你要返回php,那么记住php是服务器端。你回复给客户了吗?如果是这样,它就不会起作用,你应该重新调查一下。客户端不会运行PHP。
为什么不使用ajax或其他东西?
编辑:
print showFiles('files/'.$type.'files/'.do_shortcode($content));
现在在showFiles函数中,将<ul>
放在返回字符串周围。这应该会让事情变得更加混乱。
EDIT-编辑:
function showFiles($path){
if ($handle = opendir($path))
{
$up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
$returnString = "<ul>"; //ADDED THIS
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)) {
returnString .= "<li><a href='/".$file."' target=_blank>".$fName."</a></li>";
}
}
}
closedir($handle);
$returnString .= "</ul>";
return $returnString;
}
}