【发布时间】:2016-03-08 12:57:44
【问题描述】:
我正在与客户的 wordpress 附属网站合作。
我们将两个插件结合使用。
- ACF(高级自定义字段)
- EasyAzon(这会通过短代码在您的帖子中插入会员链接)
我已经为此短代码设置了自定义字段,并像这样将其编码到我的它们中。
<div class="top-link"> <?php the_field('link-button'); ?></div>
这个输出:
“<div class="top-link">
<a href="http://www.theaffiliatelink.com/path/to/product">Buy Now</a>
</div>”
现在我想在页面底部使用相同的功能“<?php the_field('link-button'); ?>”。
但是我希望它在锚标记中显示来自另一个 ACF 字段“<?php the_field('bottom-link'); ?>”的文本。
但如果该字段中没有文本,我希望它使用文本“点击在亚马逊上购买”
这就是我所拥有的:
<div class="top-link"><?php the_field('link-button'); ?></div>
<!-- The post content -->
<div class="bottom-link"> <?php the_field('link-button'); ?></div>
<?php
$link = get_field('bottom-link');
if (get_field('bottom-link') != '') {
$link = get_field('bottom-link');
}
else {
$link = "Click to Buy Amazon";
}
?>
<script>
var link = '<?php echo $link; ?>';
jQuery( document ).ready(function() {
jQuery('.bottom-link a').text(link);
});
</script>
我对这两种语言都很陌生,可能会使事情复杂化很多。任何想法都会有所帮助!
如果您有兴趣查看,这里是开发网站。
【问题讨论】:
-
这两种情况下链接的 URL 是否保持不变?你还有办法在变量中获取 URL 吗?
-
URL 由短代码连同几个数据属性和文本创建。我已经阅读了有关 DOMXPath for php 以查找特定 atts 的内容,但我的测试没有使用它。
标签: php jquery wordpress advanced-custom-fields