我有一个自定义帖子类型,模板名为single-posttype.php
. 我使用高级自定义字段插件来显示某些信息(价格和实际产品的链接)。
所有这些都很有效。但是,我想获取我在自定义字段中输入的链接,并对其进行修改,以删除任何查询字符串并附加我的附属代码。我有一个函数parse_url()
对我来说效果很好。问题是,当我尝试在页面的其他部分显示字符串时,它没有显示该字符串。
以下是我的模板的完整代码:http://pastebin.com/SrWrQ15L
下面是所讨论的函数以及我如何调用它:
$theme_link_raw = get_field(\'theme_link\');
$preview_link_raw = get_field(\'theme_preview_link\');
$author_link_raw = get_field(\'author_link\');
$theme_parts = parse_url("$theme_link_raw");
$preview_parts = parse_url("$preview_link_raw");
$author_parts = parse_url("$author_link_raw");
$theme_link = $theme_parts[\'scheme\'] . "://" . $theme_parts[\'host\'] . $theme_parts[\'path\'];
$preview_link = $preview_parts[\'scheme\'] . "://" . $preview_parts[\'host\'] . $preview_parts[\'path\'];
$author_link = $author_parts[\'scheme\'] . "://" . $author_parts[\'host\'] . $author_parts[\'path\'];
function affiliatize($link, $parts) {
switch ($parts[\'host\']) {
case "themeforest.net":
echo $link . "?ref=gorrillamcd";
break;
case "www.elegantthemes.com":
echo "http://www.elegantthemes.com/affiliates/idevaffiliate.php?id=22953" . "&" . $link;
break;
case "themefuse.com":
echo $link . "?r=42892";
break;
default:
echo $link;
break;
}
}
$theme_affiliate = affiliatize($theme_link_raw, $theme_parts);
$theme_affiliate_preview = affiliatize($preview_link_raw, $preview_parts);
$theme_author = affiliatize($author_link_raw, $author_parts);
我知道它可能并不完美,但我对PHP不太熟悉(我自己也是一个ruby爱好者),所以我想有一个简单的问题我没有看到。
Notice also this is a temporary hack. I plan to make it into a plugin, but that will take more time and learning and I\'d like to go ahead and get it working now.