Skip to content

Instantly share code, notes, and snippets.

@itsViney
Last active March 26, 2022 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsViney/d4561bed05c711f3e99758484eb0c625 to your computer and use it in GitHub Desktop.
Save itsViney/d4561bed05c711f3e99758484eb0c625 to your computer and use it in GitHub Desktop.
Wordpress shortcode based on Advanced Custom Fields' standard shortcode that adds the ability to specify which part of a 'link' field to return. Options for 'element' are url, title or target.
/* Enhanced ACF link shortcode. Return format must be array for this to work
Element can be url, title or target
Example: [acf_link field="my_link_field" post_id="1000" element="url"] (returns the URL of the link)
Example: [acf_link field="my_link_field" post_id="1000" element="title"] (returns the title of the link)
*/
function acf_link_shortcode($atts)
{
extract(shortcode_atts(array(
'field' => '',
'post_id' => false,
'element' => 'url'
), $atts));
$link = get_field($field, $post_id);
$value = $link[$element];
return $value;
}
add_shortcode('acf_link', 'acf_link_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment