特定のディレクトリのファイルのリストをリンクとして表示する機能があります。次に、関数を表示するためのショートコードがありますが、インライン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');
コンテンツ領域では、ユーザーが生成したメタを使用してショートコードを入力できるため、取得元のディレクトリはログインしているユーザーと一致します。
私は約 6 つの異なることを試しましたが、インライン 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');