簡體   English   中英

Wordpress - 嵌套的短代碼渲染

[英]Wordpress - Nested shortcodes rendering

首先,我知道我的問題與聖人項目有什么關系,但我知道你的高級wordpress知識可以幫助我完成這個。

ps我一直在使用鼠尾草腳手架創建我的短代碼。

我為嵌套使用構建了兩個短代碼:

[outdoor]
    [outdoor_item title="My title 1" color="orange"]<strong>The</strong> 1st Content[/outdoor]
    [outdoor_item title="My title 2" color="blue"]<strong>The</strong> 2nd Content[/outdoor]
    [outdoor_item title="My title 3" color="green"]<strong>The</strong> 3rd Content[/outdoor]
    [outdoor_item title="My title 4" color="red"]<strong>The</strong> 4th Content[/outdoor]
[/outdoor]

經過很長時間在stackoverflow和google中搜索一些東西我發現我必須在我的RAW outdoor短內容中應用do_shortcode() ......這樣的事情:

add_shortcode('outdoor', function($atts, $content, $tag) {
    return '<div class="outdoor">' . do_shortcode($content) . '</div>';
});

然后我的問題出來了... 函數do_shortcode()只渲染我的第一個outdoor_item並忽略其他outdoor_item 它輸出這樣的東西:

<div class="outdoor">
    <div class="outdoor-item" style="background-color: orange">
        <h1>My title 1</h1>
        <div class="outdoor-item-content">
            <strong>The</strong> 1st Content
        </div>
    </div>
</div>

ps我嘗試過do_shortcode()apply_filters('the_content', $content) ,甚至在每個outdoor_item之后調用wp_reset_postdata() 我還能嘗試什么?

最近我有類似的問題。 我需要將元素包裝到引導表中,因此短代碼應該如下:

[table]
  [fb_review href="" name="" date=""][/fb_review]
  [fb_review href="" name="" date=""][/fb_review]
  [fb_review href="" name="" date=""][/fb_review]
  [fb_review href="" name="" date=""][/fb_review]
  [fb_review href="" name="" date=""][/fb_review]
[/table]

這應該產生以下html:

<div class="row">
  <div class="col-md-6"><div class="fb-review">Some content</div></div>
  <div class="col-md-6"><div class="fb-review">Some content</div></div>
</div>
<div class="row">
  <div class="col-md-6"><div class="fb-review">Some content</div></div>
  <div class="col-md-6"><div class="fb-review">Some content</div></div>
</div>
<div class="row">
  <div class="col-md-6"><div class="fb-review">Some content</div></div>
</div>

我尋找了不同的方法如何處理這個,但后來決定自己解析$content字符串

這是functions.php的一部分:

//this function just wraps the compiled nested shortcode elements
function responsive_table($attrs, $items = []) {
  if (count($items) > 0) {
    $result = '';
    $counter = 0;

    foreach ($items as $item) {
      if (!($counter % 2)) {
        $result .= '<div class="row">';
      }
      $result .= '<div class="col-md-6">' . $item . '</div>';
      if ($counter % 2) {
        $result .= '</div>';
      }
      $counter++;
    }
    if ($counter % 2) {
      $result .= '</div>';
    }
    return $result;
  } else {
    return '';
  }
}

// this function parses the content, fetches internal shortcodes, compiles them and passes to responsive_table
function responsive_table_for($attrs, $content=null) {
  if ($content != null && $content != '') {
    $content_matches = [];
    preg_match_all('/\[.+?\[.+?\]/', $content, $content_matches); // TODO: this doesn't support nested shortcodes
    $content_array = [];
    foreach ($content_matches[0] as $item) {
      $content_array[] = do_shortcode($item);
    }
    return responsive_table($attrs, $content_array);
  } else {
    return '';
  }
}

add_shortcode('table', 'responsive_table_for');

也許這種方式不是最好的,但它對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM