$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to display a function inside shortcode tags|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to display a function inside shortcode tags

matteradmin11PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far