The intention is to generate a html table with information retrieved by the WordPress function get_terms. It creates an tabulated icon legend, and presents it in html. Here, I am creating a short-code to call on this function where needed.
However, new to PHP programming, I don't know how to implement this correctly, as I am facing a constraint in PHP programming in which a function cannot return multiple values. Please amend the following code to implement its intention, which I believe is clear, even though the code is invalid.
<?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');
?>
I can see what you're trying to do here. Instead of a return at each step (which you have discovered does not work properly), you should be creating a variable called something like $output = "";, then in place of each return, append to the output string using the .= operator like $output .= '< new stuff ...>';
Finally, at the end of the function return $output; to return the full string of HTML you've constructed.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With