如何在涉及多个 get_terms() 的 WordPress 函数中 return 多个值

How to return multiple values in a WordPress function involving multiple get_terms()

目的是用 WordPress 函数 get_terms 检索到的信息生成一个 html table。它创建了一个表格图标图例,并将其呈现在 html 中。在这里,我正在创建一个短代码以在需要时调用此函数。

但是,作为 PHP 编程的新手,我不知道如何正确实现它,因为我在 PHP 编程中面临一个函数不能 return 的约束多个值。请修改以下代码以实现其意图,我相信这是明确的,即使代码无效。

<?php
// Custom function to compile post icons legend table - added by you 18 Dec 2017
function compile_post_icons_legend_table() {
    return'<div class="narrowed">
    <h2 class="right-widget-title">Post Icons Legend</h2>
    <table class="post-icons-legend">';

    $terms = get_terms([
        'taxonomy' => 'std_or_youtube',
        'hide_empty' => false,
    ]);
    foreach ( $terms as $term ) {
        return '<tr><td><div class="small-svg-icon-container"><svg><use width = "24" height ="24" xlink:href="https://www.ashenglowgaming.com/wp-content/plugins/wp-svg-spritemap-master/defs.svg#:'.$term->slug.'"></svg></div></td><td>'.$term->description.'</td></tr>';
}

    $terms = get_terms([
        'taxonomy' => 'content',
        'hide_empty' => false,
    ]);
    foreach ( $terms as $term ) {
        return '<tr><td><div class="small-svg-icon-container"><svg><use width = "24" height ="24" xlink:href="https://www.ashenglowgaming.com/wp-content/plugins/wp-svg-spritemap-master/defs.svg#:'.$term->slug.'"></svg></div></td><td>'.$term->description.'</td></tr>';
}
    return
    '</table>
    </div>';
}
add_shortcode('post_icons_legend_table', 'compile_post_icons_legend_table');
?>

我可以看到你在这里想做什么。您应该创建一个名为 $output = ""; 之类的变量,而不是在每个步骤中使用 return(您发现它无法正常工作),然后代替每个 return,附加到使用 .= 运算符的输出字符串,如 $output .= '< new stuff ...>';

最后,在函数的末尾 return $output; 到 return 您构建的 HTML 的完整字符串。

function compile_post_icons_legend_table() {
   // Initialize a variable to build your output string
   $output = "";

   // Its starting value...
   $output = '<div class="narrowed">
    <h2 class="right-widget-title">Post Icons Legend</h2>
    <table class="post-icons-legend">';

    $terms = get_terms([
        'taxonomy' => 'std_or_youtube',
        'hide_empty' => false,
    ]);

    foreach ( $terms as $term ) {
        // Add to the string rather than return it
        $output .= '<tr><td><div class="small-svg-icon-container"><svg><use width = "24" height ="24" xlink:href="https://www.ashenglowgaming.com/wp-content/plugins/wp-svg-spritemap-master/defs.svg#:'.$term->slug.'"></svg></div></td><td>'.$term->description.'</td></tr>';
    }

    $terms = get_terms([
        'taxonomy' => 'content',
        'hide_empty' => false,
    ]);

    foreach ( $terms as $term ) {
        // Again, add to it rather than return
        $output .= '<tr><td><div class="small-svg-icon-container"><svg><use width = "24" height ="24" xlink:href="https://www.ashenglowgaming.com/wp-content/plugins/wp-svg-spritemap-master/defs.svg#:'.$term->slug.'"></svg></div></td><td>'.$term->description.'</td></tr>';
    }
    // Final closing html tags appended...
    $ouptut .= '
      </table>
    </div>';

    // Finally, return the whole HTML string you've built
    return $output;
}