I need to display a function inside shortcode tags, but my function displays outside shortcode tags. How could I display my function inside shortcode tags?
my function:
function myFunction()
{
$entries = get_post_meta(get_the_ID(), '_kad_repeat_group', true);
if (is_countable($entries) && count($entries) > 0) :
foreach ((array) $entries as $key => $entry) {
$title = isset( $entry['_kad_title_ekstra'] ) ? esc_attr($entry['_kad_title_ekstra']) : '';
echo '<h4>'.$title.'</h4>';
}
endif;
}
my shortcode:
<?php
echo do_shortcode( '[accordions][accordion title="'. get_the_title() .'"]'.myFunction().'[/accordion][/accordions]' );
?>
result:
I need to display a function inside shortcode tags, but my function displays outside shortcode tags. How could I display my function inside shortcode tags?
my function:
function myFunction()
{
$entries = get_post_meta(get_the_ID(), '_kad_repeat_group', true);
if (is_countable($entries) && count($entries) > 0) :
foreach ((array) $entries as $key => $entry) {
$title = isset( $entry['_kad_title_ekstra'] ) ? esc_attr($entry['_kad_title_ekstra']) : '';
echo '<h4>'.$title.'</h4>';
}
endif;
}
my shortcode:
<?php
echo do_shortcode( '[accordions][accordion title="'. get_the_title() .'"]'.myFunction().'[/accordion][/accordions]' );
?>
result:
I have modified the code...
function myFunction()
{
$output = '';
$entries = get_post_meta(get_the_ID(), '_kad_repeat_group', true);
if (is_countable($entries) && count($entries) > 0) :
foreach ((array) $entries as $key => $entry) {
$title = isset( $entry['_kad_title_ekstra'] ) ? esc_attr($entry['_kad_title_ekstra']) : '';
$output .= '<h4>'.$title.'</h4>';
}
endif;
return $output;
}
$output[] = '<h4 class="listHeading"><i class="fa '.$layout.'"></i>'.$title.'</h4>'; $output[] .= '<div class="ngElements">'; $output[] .= $description; $output[] .= $wysiwyg; $output[] .= $img; $output[] .= $caption; $output[] .= '</div>'; return ''. implode( $output ) .'';
– user9637601 Commented Apr 25 at 15:03