在wordpress中的短代码之间添加php代码


Add php code in between shortcode in wordpress

我正在尝试在wordpress中的两个短代码之间添加php代码,当中间的内容是文本时,我可以使用以下代码。

<?php echo do_shortcode('[tab]
[tab_item title="ITEM_TITLE"]ADD_CONTENT_HERE[/tab_item]
[tab_item title="ITEM_TITLE"]ADD_CONTENT_HERE[/tab_item]
[tab_item title="ITEM_TITLE"]ADD_CONTENT_HERE[/tab_item]
[/tab]'); 
?>

我想添加以下php代码来生成选项卡中的内容。

<?php 
 $args = array('post_type' => 'package','package-category'=>'South Africa',    'posts_per_page'=>6 );
 $loop = new WP_Query( $args );
 while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="four columns gdl-package-grid2">
 <div class="package-content-wrapper">
             <?php the_post_thumbnail(); ?>
 </div>
</div>

这可能吗?我该怎么做?

尝试使用字符串运算符

echo do_shortcode('[tab]
[tab_item title="ITEM_TITLE"]' . $something . '[/tab_item]
[tab_item title="ITEM_TITLE"]' . $something . '[/tab_item]
[tab_item title="ITEM_TITLE"]' . $something . '[/tab_item]
[/tab]'); 

EDIT-这应该可以工作,但是您可能会在循环中使用[tab][/tab]短代码时遇到问题。最简单的方法(如果可能的话(是对循环中的[tab][/tab]输出进行硬编码。希望这将帮助你走出

<?php
$args = array('post_type' => 'package', 'package-category' => 'South Africa', 'posts_per_page' => 6);
$loop = new WP_Query($args);
while($loop->have_posts()) : $loop->the_post();
    $thumbnail = get_the_post_thumbnail();
    $sc_content = <<<SCC
<div class="four columns gdl-package-grid2">
        <div class="package-content-wrapper">
            $thumbnail
        </div>
    </div>
SCC;
/**
 * The above line ("SCC;") MUST NOT have any other characters before it.
 * No spaces or tabs (indentation) can be before or after it,
 * those 4 characters must be the only characters on the line
 */
    echo do_shortcode('[tab_item title="ITEM_TITLE"]' . $sc_content . '[/tab_item]');

endwhile;
?>