Php 只返回post中的短代码?
是否可以只从帖子中筛选出短代码,然后运行短代码 我的页面如下所示:Php 只返回post中的短代码?,php,wordpress,Php,Wordpress,是否可以只从帖子中筛选出短代码,然后运行短代码 我的页面如下所示: [summary img="images/latest.jpg"] This here is the summary [/summary] Lots of text here... 我只想在特定的页面上显示快捷代码 尝试使用正则表达式,但它们似乎不起作用: $the_query = new WP_Query('category_name=projects&showposts=1'); //loop
[summary img="images/latest.jpg"]
This here is the summary
[/summary]
Lots of text here...
我只想在特定的页面上显示快捷代码
尝试使用正则表达式,但它们似乎不起作用:
$the_query = new WP_Query('category_name=projects&showposts=1');
//loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<b>';
the_title();
echo '</b>';
echo '<p>';
$string = get_the_content();
if (preg_match("[\\[[a-zA-Z]+\\][a-zA-Z ]*\\[\/[a-zA-Z]+\\]]", $string , $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
echo '</p>';
endwhile;
我看到wordpress有条带化短代码的功能,但不用于条带内容。所以我替换了整篇文章中剥离的内容字符串,只得到了短代码。唯一的缺点是短代码必须在文章的开头。使用这个正则表达式
preg\u match('/\[summary[^\]*](.*)\[\/summary[^\]*]/uis',$string,$matches)
$match[1]应该是您的文本
编辑:确定以匹配任何标记组合
使用
/\[([^\]+)\](.*?\[\/\1\]/uis
但是如果您有嵌套的标记,您可能需要再次递归地解析匹配项。但是,如果它是wordpress免费文本,我想你可能会遇到太复杂的情况,一个简单的脚本可以处理你可以使用
get\u shortcode\u regex()
函数来获取你博客中所有注册的shotcode的regex,将其应用到内容中,以获得在该内容中找到的一系列短代码,然后调用相应的回调函数来处理所需的回调函数
所以在一个循环的某个地方,它会是这样的:
$pattern = get_shortcode_regex();
$matches = array();
preg_match_all("/$pattern/s", get_the_content(), $matches);
var_dump($matches); //Nested array of matches
//Do the first shortcode
echo preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $matches[0][0] );
这将执行文章中找到的第一个短代码,显然在实践中,您希望检查是否有一个 更好的答案是:
$pattern = get_shortcode_regex();
preg_match('/'.$pattern.'/s', $post->post_content, $matches);
if (is_array($matches) && $matches[2] == 'the_shortcode_name') {
$shortcode = $matches[0];
echo do_shortcode($shortcode);
}
它将在帖子内容中搜索一个名为“theu shortcode\u name”的短码。如果找到它,它将在$matches变量中存储短代码。从那里运行起来很容易。我对这些答案有以下问题:
// Return all shortcodes from the post
function _get_shortcodes( $the_content ) {
$shortcode = "";
$pattern = get_shortcode_regex();
preg_match_all('/'.$pattern.'/uis', $the_content, $matches);
for ( $i=0; $i < 40; $i++ ) {
if ( isset( $matches[0][$i] ) ) {
$shortcode .= $matches[0][$i];
}
}
return $shortcode;
}
//返回post中的所有短代码
函数_获取_短代码(_内容){
$shortcode=“”;
$pattern=get_shortcode_regex();
preg_match_all('/'.$pattern.'/ui',$the_content,$matches);
对于($i=0;$i<40;$i++){
if(isset($matches[0][$i])){
$shortcode.=$matches[0][$i];
}
}
返回$shortcode;
}
像这样使用它:
<?php echo do_shortcode( _get_shortcodes( get_the_content() ) ) ?>
在内容上的任何位置搜索快捷码位置 示例:
[my_shortcode]
[my_shortcode_2]
$content = $post->post_content;
$my_shortcode = get_shortcode('my_shortcode',$content);
$my_shortcode_2 = get_shortcode('my_shortcode_2',$content);
echo $my_shortcode;
if($my_shortcode){ // if shortcode#1 found so echo shortcode#2 after
echo $my_shortcode_2;
}
或
Functions.php
function get_shortcode($code,$content) {
$pattern = get_shortcode_regex();
preg_match_all('/'.$pattern.'/s',$content,$matches);
if(is_array($matches) && isset($matches[2]) && in_array($code,$matches[2])) {
$index = array_search($code,$matches[2]);
$shortcode = $matches[0][$index];;
return do_shortcode($shortcode);
} else {
return false;
}
}
使用示例:
[my_shortcode]
[my_shortcode_2]
$content = $post->post_content;
$my_shortcode = get_shortcode('my_shortcode',$content);
$my_shortcode_2 = get_shortcode('my_shortcode_2',$content);
echo $my_shortcode;
if($my_shortcode){ // if shortcode#1 found so echo shortcode#2 after
echo $my_shortcode_2;
}
是的,但并不适用于所有短代码(仅适用于摘要),在检查短代码名称之前,它必须能够在语句中匹配无限数量的短代码参数+1,
isset($matches[2])