计算嵌套的简码,然后为下一个父简码重置
Count nested shortcodes, then reset for next parent shortcode
一个让我困惑的有趣问题,所以我知道如何使用静态计数器计算页面上调用短代码的次数。但是,我想在调用父简码时重置计数器:
<?php function gw_row($atts, $content = null) {
return '<div class="gw-row">'.do_shortcode($content).'</div>';
} add_shortcode("gw-row", "gw_row");
function gw_column($atts, $content = null) {
static $count = 0; $count++; if($count > 5){$count = 0}
return '<div class="column">'.do_shortcode( $content ).'</div>';
} add_shortcode("gw-column", "gw_column");?>
所以进一步解释,页面上会有多个[gw-row]
简码,这是'Parent Shortcode'。在此短代码中,我使用 [gw-column]
。我想计算每个父短代码中页面上使用了多少列。
示例:
[gw-row]
[gw-column]$Count = 1[/gw-column]
[gw-column]$Count = 2[/gw-column]
[/gw-row]
Reset $count
[gw-row]
[gw-column]$Count = 1[/gw-column]
[gw-column]$Count = 2[/gw-column]
[gw-column]$Count = 3[/gw-column]
[/gw-row]
Reset $count
我考虑过将 [gw-row]
设为对象并在 <div>
关闭后立即重置 $count
这可能可行,但我想知道是否有更好的方法?
我的想法:
<?php function gw_row($atts, $content = null) {
global $count;
ob_start();?>
<div class="gw-row">
<?php do_shortcode($content);?>
</div><?php $count = 0;?>
<?php return ob_clean();
} add_shortcode("gw-row", "gw_row");
function gw_column($atts, $content = null) {
$count++; if($count > 5){$count = 0}
return '<div class="column">'.do_shortcode( $content ).'</div>';
} add_shortcode("gw-column", "gw_column");?>
好吧,我真的是想多了,解决方案非常简单并且接近我的建议。
只需全局定义 $count
并在 [gw-row]
短代码中将其设置为 0。这将在每次调用时重置它:
<?php function gw_row($atts, $content = null) {
global $count; $count = 0;
return '<div class="gw-row">'.do_shortcode($content).'</div>';
} add_shortcode("gw-row", "gw_row");
function gw_column($atts, $content = null) {
global $count; $count++; if($count > 5){$count = 0}
return '<div class="column">'.do_shortcode( $content ).'</div>';
} add_shortcode("gw-column", "gw_column");?>
一个让我困惑的有趣问题,所以我知道如何使用静态计数器计算页面上调用短代码的次数。但是,我想在调用父简码时重置计数器:
<?php function gw_row($atts, $content = null) {
return '<div class="gw-row">'.do_shortcode($content).'</div>';
} add_shortcode("gw-row", "gw_row");
function gw_column($atts, $content = null) {
static $count = 0; $count++; if($count > 5){$count = 0}
return '<div class="column">'.do_shortcode( $content ).'</div>';
} add_shortcode("gw-column", "gw_column");?>
所以进一步解释,页面上会有多个[gw-row]
简码,这是'Parent Shortcode'。在此短代码中,我使用 [gw-column]
。我想计算每个父短代码中页面上使用了多少列。
示例:
[gw-row]
[gw-column]$Count = 1[/gw-column]
[gw-column]$Count = 2[/gw-column]
[/gw-row]
Reset $count
[gw-row]
[gw-column]$Count = 1[/gw-column]
[gw-column]$Count = 2[/gw-column]
[gw-column]$Count = 3[/gw-column]
[/gw-row]
Reset $count
我考虑过将 [gw-row]
设为对象并在 <div>
关闭后立即重置 $count
这可能可行,但我想知道是否有更好的方法?
我的想法:
<?php function gw_row($atts, $content = null) {
global $count;
ob_start();?>
<div class="gw-row">
<?php do_shortcode($content);?>
</div><?php $count = 0;?>
<?php return ob_clean();
} add_shortcode("gw-row", "gw_row");
function gw_column($atts, $content = null) {
$count++; if($count > 5){$count = 0}
return '<div class="column">'.do_shortcode( $content ).'</div>';
} add_shortcode("gw-column", "gw_column");?>
好吧,我真的是想多了,解决方案非常简单并且接近我的建议。
只需全局定义 $count
并在 [gw-row]
短代码中将其设置为 0。这将在每次调用时重置它:
<?php function gw_row($atts, $content = null) {
global $count; $count = 0;
return '<div class="gw-row">'.do_shortcode($content).'</div>';
} add_shortcode("gw-row", "gw_row");
function gw_column($atts, $content = null) {
global $count; $count++; if($count > 5){$count = 0}
return '<div class="column">'.do_shortcode( $content ).'</div>';
} add_shortcode("gw-column", "gw_column");?>