【问题标题】:Use WP Shortcode as an attribute in another Shortcode使用 WP Shortcode 作为另一个 Shortcode 中的属性
【发布时间】:2015-05-27 18:15:23
【问题描述】:

我正在尝试将高级自定义字段插件简码用作另一个简码中的属性。

制作我的主题的公司目前不提供定制支持。

原始简码列在此

public function plan( $atts, $content )
    {
        $html = array();

        extract( shortcode_atts( array(
            'price'     => '',
            'price_info'    => '',
            'type'  => '',
            'delay'     => '',
            'class'     => '',
        ), $atts ) ); 

        $html[] = '<div class="' . ( $class ) . '">';
        $html[] =   '<div class="plan has-animation" data-delay="' . ( (int)$delay ) . '">';
        $html[] =       '<div class="plan-container">';
        $html[] =           '<ins class="plan-price">' .( $price ) . '</ins>';
        $html[] =           '<span class="price-info">' . ( $price_info ) . '</span>';
        $html[] =           '<h2 class="second_color">' . ( $type ) . '</h2>';

        $html[] =           do_shortcode( $content );

        $html[] =       '</div>';
        $html[] =   '</div>';
        $html[] = '</div>';

        return implode("\n", $html); 
    }

当我调用简码 [pt_plan price="$12.99" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"] 时,我想让价格属性允许简码作为值来利用高级自定义字段。

但是,如果我将 [acf field='session_price_1'] 作为属性值,像这样,[pt_plan price="[acf field='session_price_1']" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"] 就好像这是一个字符串并破坏了原始的 pt_plan 短代码。

有人能指出我正确的方向吗?我以为是在计划简码中将do_shortcode() 添加到 $price 变量中,但它不起作用。

希望这一切都有意义,并在此先感谢!

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    我真的让它工作了。 因为简码在 WP 中的工作方式,嵌套简码的结尾 ] 关闭了原始简码,把一切都搞砸了。 为了解决这个问题,我做了以下事情:

    $html[] = '<div class="plan-price">'.do_shortcode('['.$price.']').'</div>';
    

    [pt_plan price="acf field='session_price_1'" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"]
    

    效果很好!

    基本上我只是在属性中输入了短代码的文本和 ' 而不是 ",在将属性值打印到 时将 [ 和 ] 连接起来,并使用 do_shortcode() 运行短代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2023-02-09
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多