【问题标题】:How do I make an advanced custom field a link?如何将高级自定义字段设为链接?
【发布时间】:2020-08-15 19:33:29
【问题描述】:

我在 WooCommerce 的产品页面上有一个自定义字段,其中包含以下代码

add_action('woocommerce_single_product_summary', 'subtitle_link', 15);

function subtitle_link() {
    global $post;
    $subtitle = get_post_meta($post->ID, 'link', true);
    if(!empty($subtitle)){

        echo __("\n<br />\n<br />Enter this competition for free:", '');
        echo '<h3>'.$subtitle.'</h3>';

    }
}

虽然它应该在前端显示,但 URL 不是链接。

【问题讨论】:

  • 那行不通。谢谢
  • echo "&lt;h3&gt;&lt;a href='" . $subtitle . "'&gt;My url&lt;/a&gt;&lt;/h3&gt;"; - w3schools.com/tags/att_a_href.asp
  • 我认为这也行不通,因为我希望现场的测试能够链接。谢谢
  • 你试过了吗?还可以查看我分享的链接,这真的很基础。
  • 我已经添加了这个,但它链接了页面上的所有内容 add_action('woocommerce_single_product_summary', 'my_custom_subtitle_link', 15);功能 my_custom_subtitle_link() { 全球 $post; $subtitle = get_post_meta($post->ID, 'link', true); if(!empty($subtitle)){ echo __("\n
    \n
    免费参赛:", '');回声''; echo "".$subtitle.''; } }

标签: php wordpress woocommerce advanced-custom-fields hook-woocommerce


【解决方案1】:

HTML 目标属性

  • _blank 在新窗口或标签页中打开链接的文档
  • _self 在被点击的同一框架中打开链接文档(这是默认设置)
  • _parent 在父框架中打开链接文档
  • _top 在整个窗口中打开链接的文档
  • framename 在命名框架中打开链接文档
function subtitle_link() {
    global $post;

    // Make sure this works!
    $subtitle = get_post_meta($post->ID, 'link', true);

    // If the above works, remove this!
    $subtitle = 'http://www.google.com';

    if( $subtitle ) {
        echo __("\n<br />\n<br />Enter this competition for free:", '');
        echo "<h3><a href='" . $subtitle . "' target='_blank'><span class='complink'>" . $subtitle . "</span></a></h3>";
    }
}
add_action('woocommerce_single_product_summary', 'subtitle_link', 15);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2020-04-26
  • 2017-12-16
  • 2014-03-14
  • 2013-07-14
  • 2016-05-31
相关资源
最近更新 更多