Custom Function for ACF Link Field

In Bricks Discord chat, a user asked:

Does anyone know how to output the label of an ACF link field through Dynamic Data?

With the Return Value of a “Link” type of ACF field set at its default “Link Array”, the array looks like this:

when the field is populated for a post like this:

For printing out the values of title and url array keys for use in Bricks via its dynamic data tags feature, we can define a custom function like this (goes in child theme’s functions.php or a code snippets plugin):

function bl_get_link( string $field_name, string $key ) {
    $field_value = get_post_meta( get_the_ID(), $field_name, true );

    if ( $field_value && isset( $field_value[$key] ) ) {
        return $field_value[$key];
    }
}

and use it in Bricks like this:

{echo:bl_get_link(page_link,title)}

{echo:bl_get_link(page_link,url)}

where page_link is the field name.

Get access to all 524 Bricks code tutorials with BricksLabs Pro

5 comments

  • Is there a way to get the title of a linked page from a Page Link flie type field in acf?

    • A

      As mentioned in the documentation at https://www.advancedcustomfields.com/resources/page-link/#template-usage

      > The Page Link field will return either a single URL or array of URLs. To get more data from a selected post, please use the post object field instead.

      it is better to use a Post Object type of field when you need other data about the linked post.

      • Thanks Sridhar, I've made that change and now, after setting up a custom function using "Filter: bricks/code/echo_function_names" I can now echo the post title in a button as required.

  • Is it gonna work if Link is inside ACF Repeater?

    • A

      Yes, it does.

      There is no need to use a custom function now.

      Title:
      {acf_event_organizers_page_link:array_value|title}

      URL:
      {acf_event_organizers_page_link:array_value|url}

      Target:
      {acf_event_organizers_page_link:array_value|target}

      where event_organizers is the Repeater name and page_link is the Link-type field's name.

Leave your comment

 

Related Tutorials..

Pro
ACF Repeater Row Count Condition in Bricks

ACF Repeater Row Count Condition in Bricks

This Pro tutorial provides the steps to output elements on single posts (of any post type) in Bricks only if a specific ACF Repeater-type of…
Categories:
Pro
Querying Posts by ACF Date Sub Field in Bricks

Querying Posts by ACF Date Sub Field in Bricks

Updated on 17 Nov 2023. This Pro tutorial shows how we can set post__in query parameter of a query loop in Bricks to an array…
Categories:
Tags:
Pro
Related Posts using ACF Post Object in Bricks

Related Posts using ACF Post Object in Bricks

Modifying the query loop to limit the posts to only related posts via an ACF Post Object custom field.
Categories:
Tags:
Pro
Google Maps JavaScript API in Bricks

Google Maps JavaScript API in Bricks

This Pro tutorial provides the steps to show post-specific maps with custom marker content in Bricks using Google's Maps JavaScript API and Advanced Custom Fields.…
Categories:
Accessing ACF Repeater Sub Fields Programmatically in Bricks Query Loop

Accessing ACF Repeater Sub Fields Programmatically in Bricks Query Loop

It is possible to output sub field's values when a Bricks query loop's type has been set to a ACF Repeater without writing code. This…
Categories:
Pro
Meta Box/ACF Images Staggered Grid Gallery in Bricks

Meta Box/ACF Images Staggered Grid Gallery in Bricks

Showing images from two Gallery-type of ACF or Image Advanced-type of Meta Box fields in a staggered grid layout.
How to conditionally load your scripts based on an ACF field

How to conditionally load your scripts based on an ACF field

This tutorial provides the PHP codes that can be pasted in order to enqueue your scripts conditionally based on the value of an ACF field…
Categories:
Pro
Category Grid with ACF Images in Bricks

Category Grid with ACF Images in Bricks

Creating a grid of post categories, each category card showing an image from an ACF field, category name & description.
Categories: