wp query - embedding shortcodes in php template

admin2025-06-06  1

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());
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749140294a316706.html

最新回复(0)