$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'); ?>wp query - embedding shortcodes in php template|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)

wp query - embedding shortcodes in php template

matteradmin9PV0评论

I am using a plugin that has shortcodes. It is called according shortcodes

[accordion]
[accordion-item title="Title of accordion item"]Drop-down content goes here.[/accordion-item]
[accordion-item title="Second accordion item"]Drop-down content goes here.[/accordion-item]
[/accordion]

I wanted put it in a wp_query, but i can't seem to figure out how to nest the shortcode. Can someone please help?
This is what I already tried:

<?php echo do_shortcode ('[accordion]'); ?>  
                <?php                                                     
                    $args = array(
                                    'posts_per_page' => '-1',
                                    'post_type' => 'post',
                                    'post_status' => 'publish',
                                    'category__in' => $quicksand_categories 
                                    );                     
                    $query = new WP_Query( $args );                                             
                    foreach ($query->posts as $item) {                      
                        $categories = wp_get_post_categories($item->ID);
                        ?>
    <?php echo do_shortcode ('[accordion-item title="'.get_the_title($item->ID).'"]'.the_content().'[/accordion-item]'); ?>                   

                    <?php  }  ?>
<?php echo do_shortcode ('[/accordion]'); ?>

I am using a plugin that has shortcodes. It is called according shortcodes

[accordion]
[accordion-item title="Title of accordion item"]Drop-down content goes here.[/accordion-item]
[accordion-item title="Second accordion item"]Drop-down content goes here.[/accordion-item]
[/accordion]

I wanted put it in a wp_query, but i can't seem to figure out how to nest the shortcode. Can someone please help?
This is what I already tried:

<?php echo do_shortcode ('[accordion]'); ?>  
                <?php                                                     
                    $args = array(
                                    'posts_per_page' => '-1',
                                    'post_type' => 'post',
                                    'post_status' => 'publish',
                                    'category__in' => $quicksand_categories 
                                    );                     
                    $query = new WP_Query( $args );                                             
                    foreach ($query->posts as $item) {                      
                        $categories = wp_get_post_categories($item->ID);
                        ?>
    <?php echo do_shortcode ('[accordion-item title="'.get_the_title($item->ID).'"]'.the_content().'[/accordion-item]'); ?>                   

                    <?php  }  ?>
<?php echo do_shortcode ('[/accordion]'); ?>
Share Improve this question asked Dec 7, 2015 at 20:51 rudtekrudtek 6,4035 gold badges30 silver badges52 bronze badges 1
  • Check out this plugin if you plan on doing something like this a lot wordpress/plugins/custom-content-shortcode/screenshots. It lets you use shortcodes in html templates and also give you virtually every wordpress php function as a shortcode. – Bryan Willis Commented Jan 12, 2016 at 10:17
Add a comment  | 

2 Answers 2

Reset to default 0

I think it needs to be like this:

<?php $output = '[accordion]'; ?>  
            <?php                                                     
                $args = array(
                                'posts_per_page' => '-1',
                                'post_type' => 'post',
                                'post_status' => 'publish',
                                'category__in' => $quicksand_categories 
                                );                     
                $query = new WP_Query( $args );                                             
                foreach ($query->posts as $item) {                      
                    $categories = wp_get_post_categories($item->ID);
                    ?>
<?php $output.='[accordion-item title="'.get_the_title($item->ID).'"]'.the_content().'[/accordion-item]'; ?>                   

                <?php  }  ?>
<?php $output .= '[/accordion]'; ?>
<?php echo do_shortcode($output); ?>

An alternative approach for using opening/closing shortcodes in a template, that doesn't require a lot of string concatenation:

<?php
// Buffer the output so that we can use [shortcodes][/shortcodes]
ob_start();
?>

<h1>A stylish template</h1>
[my_whizzy_shortcode param="awesome"]
<p><?= the_content() ?></p>
<div>More template html, php tags, etc</div>
[/my_whizzy_shortcode]

<?php
// Now write out the template, including the parsed shortcodes
echo do_shortcode(ob_get_clean());
?>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far