将实例编号传递给短码子对象

时间:2015-12-09 作者:Peter Westerlund

我在传递shortcode的实例变量时遇到问题[answers] 一直到孩子们[ans]. 无法使用全局,因为这样会失去其价值。

代码如下:

function my_answers_shortcode($params, $content = null)
{
    static $instance = 0;
    $instance++;

    $output = "<div class=\'answers-wrapper\'>
                    <div id=\'answers-{$instance}\' class=\'answers\'>";

    add_shortcode(\'ans\', \'my_ans_shortcode\');
    $output .= do_shortcode($content);

    $output .= "    </div>
                </div>";

    return $output;
}
add_shortcode(\'answers\', \'my_answers_shortcode\');

function my_ans_shortcode($atts, $content = null)
{
    global $post;
    static $ans = 0;
    $ans++;

    $output = " 
                <dl class=\'answer-item\'>";

    $output .= "    <dd class=\'wp-caption-text answer-caption\'>
                        <a href=\'?ins=".$instance."&ans=".$ans."\'>".$content."</a> ".($_SESSION[\'page_\'.($post->ID).\'_ins_\'.$instance.\'_ans_\'.$ans] == \'correct\' ? "Correct!" : "")."
                    </dd>";

    $output .= "
                </dl>
                ";

    return $output;
}

1 个回复
最合适的回答,由SO网友:birgire 整理而成

您可以将其包装在类中。

下面是一个示例:

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

相关推荐

Namespaced shortcode?

我正在改造一个旧的WP站点,该站点有许多自定义的短代码,显然由于代码当前的组织方式,这些短代码在性能方面付出了代价。当然,我可以修复优化不好的代码,使用十几个短代码,并且一天就可以完成,但我想知道如何更好地组织它们。根据WordPress\'documentation, 建议将它们放在插件中并在上初始化init. 我们可以通过这样“命名”它们来减少这个钩子中的负载吗?[com.company shortcode attr=\"attr\" prop=\"prop\"] 有人尝试过这样的解决方案吗