Support

Account

Home Forums General Issues Page Link field content in a function

Solved

Page Link field content in a function

  • I’m trying to display a custom button right after the add to cart button in the WooCommerce product page.

    For the moment it’s purely static:

    add_action( 'woocommerce_after_add_to_cart_form', 'add_content_after_addtocart_button_func' );
    
    function add_content_after_addtocart_button_func() {
    
    echo '<a href="#"><span>Button text</span></a>';
    
    }

    But I’d like to be able to choose a custom URL in the product page editor. I’ve used the ACF Page Link field but I’ve no idea how to use it in a function.

    Thanks for your help

  • Any idea for achieving this?

  • 
    $link = get_field('your_link_field_name');
    if ($link) {
      echo '<a href="'.$link['url'].'"><span>'.$link['title'].'</span></a>';
    }
    

    https://www.advancedcustomfields.com/resources/link/

  • Thanks John!

    This works for the link field only, not the link to page/post but it’s fine for me as well.

  • Sorry, I thought this was for the link field and not the page link field.

    The page link field doesn’t return link text and only returns the URL and you’d need to supply the text some other way.

    
    $link = get_field('your_link_field_name');
    if ($link) {
      echo '<a href="'.$link.'"><span>Button text</span></a>';
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Page Link field content in a function’ is closed to new replies.