26th Apr '23
/
6 comments

Custom Function for ACF Link Field

Update: See https://brickslabs.com/custom-function-for-acf-link-field/#comment-863


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 626 Bricks code tutorials with BricksLabs Pro

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

      • Hunter

        I've been looking for the syntax for too long. This fixed my issue. Thank you Sridhar

Leave a Reply to Rich (Cancel Reply)

 

Related Tutorials..

Pro
Querying Posts by Date/Datetime Custom Fields in Bricks

Querying Posts by Date/Datetime Custom Fields in Bricks

This Pro tutorial shows how to configure ACF and Meta Box to query posts in Bricks by custom field(s) of the type Date/Date Time Picker…
Categories:
Tags:
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

This tutorial provides the PHP & JS codes that can be pasted in order to create a flying effect on map markers each time your…
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:
ACF Checkbox Items with Custom SVG Bullets in Bricks

ACF Checkbox Items with Custom SVG Bullets in Bricks

We walk through outputting list items with SVG icons based on the choice of a ACF Checkbox field.
Categories:
Tags:
Modifying ACF Field Value Before It Is Output

Modifying ACF Field Value Before It Is Output

Consider the scenario where a Page has a "Page Fields" field group created with ACF Pro like this: Field name: scientific_coordinators (type Repeater)|__ Sub field…
Categories:
ACF Repeater Sub Field Values as Tabs in Bricks

ACF Repeater Sub Field Values as Tabs in Bricks

Bricks' query loop supports ACF Repeaters and Meta Box Groups out of the box besides posts, users and terms. This tutorial shows how we can…
Categories:
Tags:
Pro
Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

On product pages, we show all the FAQs associated with the current WooCommerce product's product categories.
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.