Home > database >  How to use a variable in an ACF template and pass it through a file?
How to use a variable in an ACF template and pass it through a file?

Time:06-08

I use wordpress and ACF. I use $link in the template, for example:

$link = get_field('custom_link');

After that, I wrote a code that checks the url for identifiers and if there are matches, then replaces $link['url'], $link['title'] and $link['target'] with "options".

Next comes:

<a href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>

This all works in a build, but I'm trying to use the same code on all links in all templates, so I created a "links.php" file and pasted my link checking code in there.

In the template, I tried to call the file with get_template_part and include. Also, I've tried include(locate_template('links.php')), but that doesn't work either.

Can someone tell me how to properly use repeatable code in ACF templates?

CodePudding user response:

Everything worked using:

include(locate_template('folder1/links.php'));

The template was located in the folder with the theme in "folder2", and the link file in "folder1". Before that, I tried:

include(locate_template(' ../folder1/links.php')); 

and various variations, but I did not think that the path is taken from the theme folder.

  • Related