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

问题描述 投票:1回答:1

目的是生成一个html表,其中包含由WordPress函数get_terms检索的信息。它会创建一个列表图标图例,并以html格式显示。在这里,我正在创建一个短代码,以便在需要时调用此函数。

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

<?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');
?>
php return shortcode
1个回答
1
投票

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

最后,在函数return $output;的末尾返回你构造的完整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;
}
© www.soinside.com 2019 - 2024. All rights reserved.