在封闭的和封闭的短码之间传递值

时间:2013-11-06 作者:Kevin Lacquement

是否有办法在封闭和封闭的短代码之间传递数据?例如,我想做如下事情:

[boxes per_row=4]
    [box]foo[/box]
    [box]bar[/box]
    [box]baz[/box]
    [box]quux[/box]
[/boxes]
并且能够让不同的盒子有不同的样式(例如class=\'first\',class=\'last\'等)。此外,我希望能够在当前行已满时创建新行。

基本上,我希望当孩子被叫来时,家长能够以某种方式被叫回。

1 个回复
SO网友:birgire

您可以尝试此插件:

/**
 * Plugin Name: My Boxes Shortcode 
 * Author: birgire
 * Author URI: http://wordpress.stackexchange.com/a/121423/26350
 */

add_action( \'plugins_loaded\', array( \'My_Boxes_Shortcode\', \'get_instance\' ) );

if( ! class_exists( \'My_Boxes_Shortcode\' ) )
{

    class My_Boxes_Shortcode
    {
        static private $instance = NULL;

        static public function get_instance() 
        {        
            if ( NULL === self::$instance )
                self::$instance = new self;

            return self::$instance;            
        }

        public function __construct() 
        {
            add_shortcode( \'boxes\', array( $this, \'boxes_callback\' ) );   
            add_shortcode( \'box\',   array( $this, \'box_callback\'   ) );   
        }        

        public function box_callback( $atts, $content ) 
        {        
            $atts = shortcode_atts( 
                        array(
                            \'class\'     => \'box\',
                    ), $atts, \'my_box_shortcode\' );

            // input
            $content               = esc_textarea( trim( $content ) );
            $atts[\'class\']         = esc_attr( $atts[\'class\'] );

            // output                       
            return sprintf( \'<div class="%s">%s</div>\', $atts[\'class\'], $content );             
        }

        public function boxes_callback( $atts, $content ) 
        {        
            $atts = shortcode_atts( 
                        array(
                            \'class\'     => \'boxes\',
                            \'per_row\'   => \'3\',
                    ), $atts, \'my_boxes_shortcode\' );

            // input
            $content               = str_replace( \'<br />\', \' \', $content );
            $atts[\'class\']         = esc_attr( $atts[\'class\'] );
            $atts[\'per_row\']       = (int) $atts[\'per_row\'];

            // if per_row is not positive then force it to the default value.
            $atts[\'per_row\'] = ( 1 > $atts[\'per_row\'] )? 3 : $atts[\'per_row\'];

            // split the content into boxes
            $content = str_replace( \'[/box]\', \'[/box]<!--x-->\', $content ); 
            $boxes = explode( \'<!--x-->\', do_shortcode( $content ) );

            // output           
            $html = \'\';

            // add a div wrapper around each row
            if( 0 < count( $boxes ) )
            {
                $html .= \'<div class="row-wrapper">\';

                foreach( $boxes as $i => $box )
                {           
                    $html .= $box;

                    // inject the row seperator
                    if(  ( $atts[\'per_row\'] - 1 ) === ( $i % $atts[\'per_row\'] ) )
                        $html .= \'</div><div class="row-wrapper">\';
                }
                $html .= \'</div>\';

                // remove the last row wrapper if it\'s empty
                if( 1 === ( count( $boxes ) % $atts[\'per_row\'] ) )
                    $html = substr( $html, 0, (-1) * strlen( \'<div class="row-wrapper"> </div> \' ) );
            }

            return sprintf( \'<!-- start boxes wrapper: -->
                                     <div class="%s">%s</div>
                             <!-- end boxes wrapper -->\', $atts[\'class\'], $html );                      
        }

    } // end class 

} // end if class ! exists
我想有更好的方法可以做到这一点,但你可以根据自己的需要调整课程。

然后这种快捷码设置:

[boxes class="myboxes" per_row="3"]
    [box class="box first"]foo[/box]
    [box]bar[/box]
    [box]baz[/box]
    [box class="box last"]quux[/box]
[/boxes]
应提供以下HTML:

<!-- start boxes wrapper: -->
<div class="myboxes">
    <div class="row-wrapper"> 
        <div class="box first">foo</div> 
        <div class="box">bar</div> 
        <div class="box">baz</div> 
    </div>
    <div class="row-wrapper"> 
        <div class="box last">quux</div>
    </div>
</div>
<!-- end boxes wrapper -->

结束

相关推荐

Custom field within shortcode

我目前正在使用“WP Simple Paypal Shopping Cart”插件建立一个简单的Paypal电子商务网站,它非常容易使用,但为了节省客户编写短代码的时间,我在后端设置了自定义字段以输入产品名称和价格TheAdd to Cart 按钮是使用一段简单的短代码生成的,如下所示:<?php echo print_wp_cart_button_for_product(\'Product Name\', Product Price); ?> 显然正在替换Product Name