我有一些帖子,其中一些包含[gravityform]
具有不同属性值的短代码。
比如,一篇博文《你好世界》包含一个短代码[gravityform id="1" title="false" description="false"]
另一篇帖子《你好,月亮》包含短代码[gravityform description="true" id="23" title="true"]
如何获取id
以编程方式设置该短代码的属性?
我有一些帖子,其中一些包含[gravityform]
具有不同属性值的短代码。
比如,一篇博文《你好世界》包含一个短代码[gravityform id="1" title="false" description="false"]
另一篇帖子《你好,月亮》包含短代码[gravityform description="true" id="23" title="true"]
如何获取id
以编程方式设置该短代码的属性?
如果帖子内容中只有1个短代码,请捕获id
使用此选项:
<?php
$text = get_the_content();
preg_match_all("\\bid="([0-9]+)\\b", $text, $matches);
var_dump($matches[0]);
?>
在您的示例中,它将输出:23
但是,如果您在同一篇文章中使用其他短代码或简单文本,并使用相同的结构id=123
, 您可能会得到意外的输出:它将只捕获在帖子中找到的第一个输出。否则,您可以尝试以下操作(未经测试):
$post_content = get_the_content();
$start = \'[gravityform id="\';
$end = \'"\';
$post_content = \' \' . $post_content;
$ini = strpos($post_content, $start);
if ($ini === 0) return \'\';
$ini += strlen($start);
$len = strpos($post_content, $end, $ini) - $ini;
$the_id = substr($post_content, $ini, $len);
echo $the_id; // 23 in your example
但是,您必须始终使用id
作为第一个参数。这是我的密码。我想按短代码显示它。$user_id = get_current_user_id(); $balance = mycred_get_users_balance( $user_id ); echo "Current Balance: ".$balance."<br><br>"; if ($_POST && $_SERVER["REQUEST_METHOD"] == &qu