我正在尝试创建一个简单的基于Wordpress简码的函数,该函数将采用定义的数字并将其转换为最终用户的本地货币。我陷入困境的是如何在帖子中搜索短代码,因为定义的数字可能会发生变化。如果有人可以让我知道如何最好地将数字提取为变量,然后我可以通过汇率函数运行它(工作正常,我已经使用自定义字段存储的数据对其进行了测试)。
{customShortcode - priceAdditions [400]}
我试图在[]
周围explode()
它,这似乎显示出希望,但我无法弄清楚如何提取它,而且有可能有多个实例与不同的数字一起使用。我认为regex
或preg_match
可能是要走的路,但我还不太明白。
如果您需要更多信息,请告诉我。提前谢谢。
担
编辑 - 适用于短代码的函数 -
$thePostContent = get_the_content($post->ID);
$thePostContent = str_replace('{customShortcode - price}',thePrice(),$thePostContent);
函数 -
function thePrice(){
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
$Exploded_URL = explode("/",$pageURL);
if ($Exploded_URL[4] == ''){$thePostIsIn = 217;}
elseif ($Exploded_URL[4] == 'productA'){$thePostIsIn = 347;}
elseif ($Exploded_URL[4] == 'productB'){$thePostIsIn = 345;}
else {$thePostIsIn = 217;}
if (empty($_COOKIE['userLocate']) || $_COOKIE['userLocate'] == 'US' || ($_COOKIE['userLocate'] != 'EU' && $_COOKIE['userLocate'] != 'AU' && $_COOKIE['userLocate'] != 'GB')){
$currencyCode = 'USD';
$currencyPrefix = '$';
}
elseif ($_COOKIE['userLocate'] == 'EU'){
$currencyCode = 'EUR';
$currencyPrefix = '€';
}
elseif ($_COOKIE['userLocate'] == 'AU'){
$currencyCode = 'AUD';
$currencyPrefix = 'A$';
}
elseif ($_COOKIE['userLocate'] == 'GB'){
$currencyCode = 'GBP';
$currencyPrefix = '£';
}
else {
$currencyCode = 'USD';
$currencyPrefix = '$';
}
$args=array(
'post_type' =>'page',
'post__in' => array($thePostIsIn)
);
$recent_posts = get_posts($args);
foreach( $recent_posts as $recent ){
$mypages = get_post( $recent->ID );
$theBaseRate = get_post_meta($recent->ID, "Payment Cost",1);
if(get_post_meta($recent->ID, "Payment Period",1)){
$payPeriod = get_post_meta($recent->ID, "Payment Period",1);
}
else{
$payPeriod = "per month";
}
$rssFeedUrl = "http://themoneyconverter.com/rss-feed/GBP/rss.xml";
$rss = simplexml_load_file($rssFeedUrl);
foreach($rss->channel->item as $feedItem){
$currency = explode('/',$feedItem->title);
if(strpos($currency[0], $currencyCode )!== false){
$content = $feedItem->description;
$content = explode('= ',$content);
$content = substr($content[1],0,7);
$theCost = $theBaseRate * $content;
$theCost = number_format($theCost, 2, '.', '');
}
}
}
echo '<p class="rentCost"><span class="rentalCost">'.$currencyPrefix.$theCost.' '.$payPeriod.'</span></p><div class="clear"></div>';
}
你应该使用WordPress内置的简码api。
在你的functions.php
function thePrice( $atts ) {
extract( shortcode_atts( array(
'price' => 0 //default price
), $atts ) );
//$price is now available and will hold the user entered price
//in the example below this would be 400
/*
Your conversion code here
*/
}
add_shortcode( 'convert_price', 'thePrice' );
如何在编辑器中使用简码
[convert_price price="400"]
现在,您只需在模板循环中使用the_content()
,短代码就会正确呈现。
你想用自定义简码去哪里,但你为什么不坚持使用Wordpress的内置短代码机制,让它做繁重的工作呢?官方文件说
"API 处理所有棘手的解析,消除了对 为每个短代码编写自定义正则表达式。助手 包括用于设置和获取默认属性的函数。
您的代码可能变为:
[priceAdditions price="400"]
只需对functions.php
进行简短的补充,您就可以获得数据。