Last active
March 26, 2022 20:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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