【问题标题】:Changing link text php from wordpress shortcode从 wordpress 简码更改链接文本 php
【发布时间】: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>

现在我想在页面底部使用相同的功能“&lt;?php the_field('link-button'); ?&gt;”。

但是我希望它在锚标记中显示来自另一个 ACF 字段“&lt;?php the_field('bottom-link'); ?&gt;”的文本。

但如果该字段中没有文本,我希望它使用文本“点击在亚马逊上购买”

这就是我所拥有的:

<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>

我对这两种语言都很陌生,可能会使事情复杂化很多。任何想法都会有所帮助!

如果您有兴趣查看,这里是开发网站。

http://tttrend.com/wtbag/gifts-for-men/general/test-post/

【问题讨论】:

  • 这两种情况下链接的 URL 是否保持不变?你还有办法在变量中获取 URL 吗?
  • URL 由短代码连同几个数据属性和文本创建。我已经阅读了有关 DOMXPath for php 以查找特定 atts 的内容,但我的测试没有使用它。

标签: php jquery wordpress advanced-custom-fields


【解决方案1】:

这是你应该做的

<?php 
    $link = get_field('bottom-link');

    if (!isset($link) || empty($link) ) {
        $link = "Click to Buy Amazon";
    }
?>

此外,如果您一遍又一遍地使用相同的字段,请尝试在变量中获取结果,然后改用该变量。使用 get_field 向数据库发送请求并反复使用它可能会降低您的应用程序的速度。

【讨论】:

  • 感谢您的意见。我试过这个建议。无论我是否填充了该字段,它都会返回“点击在亚马逊上购买”。 get_field() 是否有可能返回一些不可见的东西?我不知道如何检查。
  • 尝试用 $link != '' 替换 empty($link),或者在这两个之外再添加一个
猜你喜欢
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 2019-08-16
相关资源
最近更新 更多