我使用的插件有短代码。它被称为根据短代码
[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]
我想把它放在wp\\u查询中,但我似乎不知道如何嵌套短代码。有人能帮忙吗<这是我已经尝试过的:
<?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]\'); ?>
最合适的回答,由SO网友:coopersita 整理而成
我认为应该这样:
<?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); ?>
SO网友:Mark Chitty
在模板中使用打开/关闭短代码的另一种方法,它不需要大量的字符串串联:
<?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());
?>