How to show link display text from a ACF in a posts element/query loop

This is the page, I have a list of resources that are a custom post type with some custom fields. The query shows post title, description, and an external website link.

The external website link is what I’m having an issue with. It links correctly but it shows the url instead of the link text (Visit https://www.smallhomealliance.com/ instead of Visit Small Home Alliance).

I made sure to include link text in the ACF field (the field type is link), but Bricks doesn’t seem to be pulling it in. Am I missing a step here?

(note that only a few of them contain the external links at the moment).

This is a current limitation of Bricks ACF integration for the link field. It always returns the URL. There is no (native) way to get a link’s title or target properties.

You could create a small helper function though and use this instead of the native tag. Something like:

function get_acf_link_title( $selector ) {
    $link = get_field( $selector );
    if ( is_array( $link ) && array_key_exists( 'title', $link ) ) {
        return $link['title'];
    }
    
    return false;
}

You can then get the title in Bricks using the dynamic echo tag:

{echo:get_acf_link_title(external_link)}

Replace external_link with the name of your ACF link field.

Thank you so much for the detailed reply.

That worked perfectly to display the title but now it’s not an external link anymore. I tried adding :link but that just links it to the single post page, not the external link.

Hey,

I was running in the same issue when trying to get the title from an ACF link field for a button element.

@aslotta, your solution is working for a basic text element, but not to populate e.g. a button title.

Backend:

Frontend (sorry, I can’t add more than one image)

As you can see, I am using the same function to get the title. Any idea why my button has no title in the frontend?

Hey @rikhen,

no problems here. See https://cln.sh/pYNgvZDz.

Are you working on a regular page or in a template?

If you can give me some more details about your exact setup or maybe even some temporary access I’m happy to have a look.

2 Likes

Hey @aslotta thanks for you fast reply.

I am working in a template and checked everything again, and it seems that just populating the button title in the preview is not working.

That’s not very developer-friendly, but it’s OK for now.

I am developing locally, so I can’t provide you with temporary access. Sorry.

Did you manage to fix this?

You can use a custom function like suggested above. However, it seems like the dev team of Bricks is adressing this issue soon:

* How to get the value (not the label) of an acf field?
* ACF Link Field enhancements

Here’s another great source for that custom function I just found on Brickslab

1 Like