从快捷码获取属性值

时间:2012-11-21 作者:Ahmad Ajmi

我有这个短代码

  [learn_more caption="something here:"]
  [/learn_more]
我想提取标题值并删除其他内容。“这里有些东西:”

这是我的代码,但它按原样打印了短代码。

 $exclude_codes = \'learn_more\';
 $wp_content = preg_replace("~(?:\\[/?)(?!(?:$exclude_codes))[^/\\]]+/?\\]~s", \'\', $wp_content); 
谢谢

2 个回复
最合适的回答,由SO网友:Ahmad Ajmi 整理而成

谢谢大家的帮助,实际上我根本没有使用regex就解决了这个问题。我将wp mpdf插件与wp event manager一起使用,当我尝试将事件页面输出为PDF时,有些内容没有显示,如标题,有些内容。。这就是问题的原因,但经过一些定制,我能够从不同的地方获取内容,问题就解决了。

来自wp mdf的代码。mpdf\\u输出函数中的php

$EM_Event = em_get_event($post->ID, \'post_id\');

$wp_content = \'\';
$header_content = \'<h1></h1>\';
$footer_content = \'\';

$header_content .= \'<h2><a href="\' . get_permalink() . \'" rel="bookmark" title="Permanent Link to \' . the_title(\'\',\'\', false) . \'">\' . the_title(\'\',\'\', false) . \'</a></h2>\';
$header_content .= \'<h3>\'.$EM_Event->output(\'#_EVENTDATES\').\'</h3>\';            
$header_content .= \'<h3>\' .$EM_Event->output(\'#_LOCATIONNAME\') . \' - \' .$EM_Event->output(\'#_LOCATIONCOUNTRY\'). \'</h3>\';

$footer_content .= \'<div id="footer">\'; 
$footer_content .= \'<strong></strong>\';
$footer_content .= "<div>";

$wp_content .= $header_content;
$wp_content .= apply_filters(\'the_content\', $wp_content);
$wp_content .= $footer_content;

SO网友:s_ha_dum

我对你的问题的理解与埃里克·霍姆斯不同。在我看来,您的快捷码在大多数情况下都需要正常工作,但您是在特殊情况下提取信息的。

短代码正则表达式很棘手。让WordPress帮你做吧。

$pattern = get_shortcode_regex();
preg_match_all("/$pattern/",$wp_content,$matches);
中存在的任何短代码的属性$wp_content 现在应该在$matches[3]. 你想做一些像。。。

$lm_shortcode = array_keys($matches[2],\'learn_more\');
if (!empty($lm_shortcode)) {
    foreach($lm_shortcode as $sc) {
      $captions[] = $matches[3][$sc];
    }
}
您仍然需要稍微清理字符串,因为其中会有一个“caption=”,但您远远领先于开始的位置,可以在设置$captions 具有str_replacepreg_replace 取决于你需要比赛的复杂程度。

Getting attribute value from shortcode

结束

相关推荐

plugin shortcode output

我有一个使用此短代码的插件:[daisy]此短代码的html输出为:<a class=\"clickable\">Click Here</a> 如果我使用快捷码来触发插件,那么插件工作得很好。它也适用于此:<?php echo do_shortcode(\'[daisy]\'); ?>通常,它还应与<a class=\"clickable\">Click Here</a> 但它不起作用,链接出现了,但如果我点击它,什么都不会发生