2

I have a function to display a list of files from a given directory as links. I then have a shortcode to display the function, but I can't figure out how to make the shortcode work without using inline php to call the function. The shortcode only works right now because I have another third-party plugin that allows inline php in the WordPress text editor.

Here's the function:

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);
   }    
}

And here's a pared down version of the shortcode function I'm using:

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');

The content area allows the user to enter a shortcode with a user generated meta so the directory it's pulling from will match the logged in user.

I've tried about six different things, and haven't been able to achieve this without calling the showFiles function using inline php. I came close once using:

$files = showFiles('files/'.$type.'files/'.do_shortcode($content).);

and

$showfiles = '<ul>'.$files.'</ul>';
 return $showfiles;

but that threw the list of files at the top of the page, I assume because the original showFiles function is echoing the html output. But if I change echo to return in the top function I get nothing.

//////////////////// FINAL WORKING CODE ///////////////////////////

With thanks to FaddishWorm for his help.

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');
4

1 回答 1

1

从函数返回某些内容不会将其回显到页面。当你调用你的函数时,你可以尝试echo showFile(blah);

如果您要返回 php,请记住 php 是服务器端。你要把这个退给客户吗?如果是这样,那么它将不起作用,您应该重新调查。客户端不会运行 PHP。

你为什么不使用ajax或其他东西?

编辑:

print showFiles('files/'.$type.'files/'.do_shortcode($content));

现在在您的 showFiles 函数中,放置<ul>返回字符串。这应该使事情变得不那么混乱。

编辑编辑:

    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;
   }    
}
于 2013-09-27T05:02:33.507 回答