【问题标题】:How to return multiple values in a WordPress function involving multiple get_terms()如何在涉及多个 get_terms() 的 WordPress 函数中返回多个值
【发布时间】:2018-05-31 07:34:10
【问题描述】:

目的是生成一个 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】:

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

    最后,在函数 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-14
      • 2021-05-13
      • 2020-05-04
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2012-06-06
      • 2019-05-10
      相关资源
      最近更新 更多