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.

Instant access to 390+ Bricks code tutorials with BricksLabs Pro

2 comments

  • 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