How to display a function inside shortcode tags

admin2025-05-31  0

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:

Share Improve this question edited Apr 25 at 13:57 mmm 3,8893 gold badges16 silver badges22 bronze badges asked Apr 25 at 13:28 user9637601user9637601 52 bronze badges 5
  • 1 to use the function in a string, the function must return the result and not display it. – mmm Commented Apr 25 at 13:55
  • you can debug this kind of issue by analysing the generated html code. – mmm Commented Apr 25 at 13:59
  • when I use return it outputs only one value of four – user9637601 Commented Apr 25 at 14:36
  • using following code I get only one value, but actually there are four $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
  • Please edit your question to include any updated code snippets; it's very difficult to read code in the comments. – Pat J Commented Apr 25 at 21:14
Add a comment  | 

1 Answer 1

Reset to default 0

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;
}

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748638196a313701.html

最新回复(0)