【问题标题】:Nesting shortcodes in Wordpress results in the inner shortcode not printing out at all在 Wordpress 中嵌套简码会导致内部简码根本不打印出来
【发布时间】:2012-12-07 15:30:38
【问题描述】:

我已经安装了 _s 主题,但几乎没有开始使用它。

我正在尝试将名为“b”的简码嵌套在名为“cb.”的简码中这是我的代码(来自functions.php 文件):

// Short Code
function box_shortcode( $atts, $content = null ) {
  extract(
      shortcode_atts(array(
          's' => '1'
          ), $atts));

  if($s == '1') { $box_classes = 'c1_4 aside'; }
  if($s == '2') { $box_classes = 'c2_4 main'; }
  if($s == '3') { $box_classes = 'c3_4'; }
  if($s == '4') { $box_classes = 'c4_4'; }

  return '<section class="' . $box_classes . '">' . $content . '</section>';
}
add_shortcode( 'b', 'box_shortcode' );

function contentblock_shortcode( $content = null ) {
  return '<div class="inner">' . do_shortcode($content) . '</div>';
}
add_shortcode( 'cb', 'contentblock_shortcode' );

add_filter('the_content', 'do_shortcode');

【问题讨论】:

  • 你能举一个例子来说明你是如何使用你的短代码的吗?

标签: php wordpress shortcode


【解决方案1】:
add_filter('the_content', 'do_shortcode');

不需要。

您的两者都应该将 atts 和 content 作为属性,并且您的 OUTER(不是内部)函数应该在其中包含 do_shortcode。最终代码应该是这样的:

function box_shortcode( $atts, $content = null ) {
  extract(
      shortcode_atts(array(
          's' => '1'
          ), $atts));

  if($s == '1') { $box_classes = 'c1_4 aside'; }
  if($s == '2') { $box_classes = 'c2_4 main'; }
  if($s == '3') { $box_classes = 'c3_4'; }
  if($s == '4') { $box_classes = 'c4_4'; }

  return '<section class="' . $box_classes . '">' . $content . '</section>';
}
add_shortcode( 'b', 'box_shortcode' );

function contentblock_shortcode( $atts, $content = null ) {
  return '<div class="inner">' . do_shortcode($content) . '</div>';
}
add_shortcode( 'cb', 'contentblock_shortcode' );

如果您将短代码用作[cb][b]content[/b][/cb] 如果您想反过来使用它们,请将do_shortcode($content) 移至函数box_shortcode( $atts, $content = null )

【讨论】:

  • 谢谢!像魅力一样工作。
【解决方案2】:

我认为问题在于您的 contentblock_shortcode 函数将 $content 作为其第一个参数,而它应该首先采用 $atts。将其更改为 function contentblock_shortcode($atts, $content = null) 应该允许内部 do_shortcode 仅将内容传递给内部部分中的 do_shortcode 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多