WordPress:从内容字段中获取所有简码

WordPress: Get all shortcodes from content field

我正在使用自定义简码在我的内容中显示 Bootstrap 模式。问题是 <div> 破坏了内容。看这里:Output content of WordPress Shortcode in text and footer

No2 我想更改短代码并仅显示模态的 link 并在内容后显示模态-<div>

为此,我检查内容字段是否有短代码,如果是这样,我会在内容后显示所有模式。

这是相关代码: (部分来自这里:)

$content = get_sub_field("textfield");
//write the begining of the shortcode
$shortcode = 'term';

$check = strpos($content,$shortcode);
if($check=== false) {
    //echo '<h1>NO Shortcode</h1>';
} else {
    //echo '<h1>HAS Shortcode</h1>';


    $str = '[term value="Term Name" id="600"][term value="Another Term" id="609"]';

    preg_match_all('~\[term value="(.+?)" id="(.+?)"]~', $str, $matches);

    var_dump($matches[2]);

    foreach($matches[2] as $match){
        echo 
            '<div class="modal fade" id="termModal_'.$match.'" tabindex="-1" role="dialog" aria-labelledby="termModal_'.$match.'_Title" aria-hidden="true">
                (rest of modal)
            </div>
        ';


    }


}

到目前为止一切正常。但现在我需要内容字段中的简码。

我不知道如何获得它们。这是我的简码:

[term value="Custom Link Title" id="123"]

我需要内容中每个短代码的 ID,并将它们存储在“$str”变量中。

这种使用 preg_match() 函数的方法应该有效:

$id = 0;
$matches = array();
preg_match('#\[term(.*) id="([0-9]{1,})"(.*)\]#', $content, $matches);
if (count($matches) > 2) {
    $id = $matches[2];
}

无论短代码属性的顺序如何,它都可以工作,但它假定您在 id 属性值周围有双引号并且该值仅由数字组成。