您可以将其包装在类中。
下面是一个示例:
class WPSE_Answers_Counting
{
private $instance_answers = 0;
private $instance_prev_answers = 0;
private $instance_answer = 0;
public function init()
{
add_shortcode( \'answers\', [ $this, \'answers_shortcode\' ] );
add_shortcode( \'ans\', [ $this, \'answer_shortcode\' ] );
}
public function answers_shortcode( $atts = [], $content = null )
{
// Output with [answers] counting
return sprintf(
"<div class=\'answers-wrapper\'>
<div id=\'answers-%d\' class=\'answers\'>
%s
</div>
</div>",
$this->instance_answers++,
do_shortcode( $content )
);
}
public function answer_shortcode( $atts = [], $content = null )
{
// Reset single [ans] counting after each [answers]
if( $this->instance_prev_answers != $this->instance_answers )
$this->instance_answer = 1;
// Output with [ans] counting
$out = sprintf(
"<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=%d&ans=%d\'>%s</a>
</dd>
</dl>",
$this->instance_answers,
$this->instance_answer++,
esc_html( $content )
);
// Update the previous [answers] instance count
$this->instance_prev_answers = $this->instance_answers;
return $out;
}
} // end class
创建一个对象实例,然后执行以下短代码破坏:
[answers][ans]a1[/ans][ans]b1[/ans][ans]c1[/ans][/answers]
[answers][ans]a2[/ans][ans]b2[/ans][ans]c2[/ans][/answers]
[answers][ans]a3[/ans][ans]b3[/ans][ans]c3[/ans][/answers]
将输出为:
<div class=\'answers-wrapper\'>
<div id=\'answers-0\' class=\'answers\'>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=1&ans=1\'>a1</a>
</dd>
</dl>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=1&ans=2\'>b1</a>
</dd>
</dl>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=1&ans=3\'>c1</a>
</dd>
</dl>
</div>
</div><br />
<div class=\'answers-wrapper\'>
<div id=\'answers-1\' class=\'answers\'>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=2&ans=1\'>a2</a>
</dd>
</dl>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=2&ans=2\'>b2</a>
</dd>
</dl>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=2&ans=3\'>c2</a>
</dd>
</dl>
</div>
</div><br />
<div class=\'answers-wrapper\'>
<div id=\'answers-2\' class=\'answers\'>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=3&ans=1\'>a3</a>
</dd>
</dl>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=3&ans=2\'>b3</a>
</dd>
</dl>
<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=3&ans=3\'>c3</a>
</dd>
</dl>
</div>
</div>
使用(希望如此)正确的实例数计算您所追求的目标。
更新:
您可能需要重置
[ans]
每次之后计数
[answers]
, 所以我调整了课程以支持这一点。
以下是静态版本:
add_shortcode( \'answers\', \'WPSE_Answers_Counting::answers_shortcode\' );
add_shortcode( \'ans\', \'WPSE_Answers_Counting::answer_shortcode\' );
class WPSE_Answers_Counting
{
private static $instance_answers = 0;
private static $instance_prev_answers = 0;
private static $instance_answer = 0;
public static function answers_shortcode( $atts = [], $content = null )
{
return sprintf(
"<div class=\'answers-wrapper\'>
<div id=\'answers-%d\' class=\'answers\'>
%s
</div>
</div>",
self::$instance_answers++,
do_shortcode( $content )
);
}
public static function answer_shortcode( $atts = [], $content = null )
{
// Reset single [ans] coutning
if( self::$instance_prev_answers != self::$instance_answers )
self::$instance_answer = 1;
$out = sprintf(
"<dl class=\'answer-item\'>
<dd class=\'wp-caption-text answer-caption\'>
<a href=\'?ins=%d&ans=%d\'>%s</a>
</dd>
</dl>",
self::$instance_answers,
self::$instance_answer++,
esc_html( $content )
);
// Update the previous instance
self::$instance_prev_answers = self::$instance_answers;
return $out;
}
} // end class