-
-
Save mvital/a005931b258309181b48f4a35ad9f8cd to your computer and use it in GitHub Desktop.
ACF Link Picker Shortcode
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
<?php | |
/** | |
* Shortcode for displaying a link from the ACF Link Picker addon. | |
* | |
* Usage: | |
* [acf_link_picker] | |
* -or- | |
* [acf_link_picker field="myfield"] | |
*/ | |
add_shortcode( 'acf_link_picker', function ( $atts, $content = '' ) { | |
/** Extract vars **/ | |
extract( shortcode_atts( array( | |
'field' => 'mylink' | |
), $atts ) ); | |
/** Get field **/ | |
$field_config = get_field($field); | |
/** Break execution if field is not set **/ | |
if(!is_array($field_config)) | |
return ''; | |
/** Set default values and merge them with the config **/ | |
$link = array_merge(array( | |
'url' => '', | |
'title' => '', | |
'target' => '_self' | |
), $field_config); | |
ob_start(); | |
?> | |
<a href="<?=$link['url']?>" target="<?=$link['target']?>"> | |
<?=$link['title']?> | |
</a> | |
<?php | |
return do_shortcode( ob_get_clean() ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment