我制作WP短代码以列出特定目录中的所有文件,以便在worpress visual composer tab shortcode [tab title="TEST ME"]SCANDIRE OUTPUT[/tab]
这是我的功能代码:
/* Start of SC Scan files */
function xs_sc_specsheets() {
$files = scandir('./documents/FD30/5_Hardware/Concort/Specsheets');
sort($files); // this does the sorting
foreach($files as $file){
if($file == ".." || $file == ".") continue; //Skip parent directory links
$xsresult = '<div class="wpb_wrapper"> <div class="column one-third"> <a class=" button button_full_width button_size_2 button_js" href="/documents/FD30/5_Hardware/Concort/Specsheets/'.$file.'"><span class="button_label">'.$file.'</span></a> </div></div>' ;
}
return do_shortcode('[tab title="TEST ME"]' . $xsresult . '[/tab]');
}
if (function_exists('xs_sc_specsheets')) {
add_shortcode( 'sc_specsheets', 'xs_sc_specsheets');
}
/* End of SC Scan files */
结果出现在我的WP内容的正确标签位置,只有一个问题,只有一个文件显示在总文件中。
我的代码中的问题在哪里?
答案 0 :(得分:2)
在循环的每个步骤中,您重置最终返回xsresult变量。试试&#34; $ xsresult。=&#34;而不是&#34; xsresult =&#34;。这是
/* Start of SC Scan files */
function xs_sc_specsheets() {
$files = scandir('./documents/FD30/5_Hardware/Concort/Specsheets');
sort($files); // this does the sorting
$xsresult='';
foreach($files as $file){
if($file == ".." || $file == ".") continue; //Skip parent directory links
$xsresult.= '<div class="wpb_wrapper"> <div class="column one-third"> <a class=" button button_full_width button_size_2 button_js" href="/documents/FD30/5_Hardware/Concort/Specsheets/'.$file.'"><span class="button_label">'.$file.'</span></a> </div></div>' ;
}
return do_shortcode('[tab title="TEST ME"]' . $xsresult . '[/tab]');
}
if (function_exists('xs_sc_specsheets')) {
add_shortcode( 'sc_specsheets', 'xs_sc_specsheets');
}
/* End of SC Scan files */