Last active
March 27, 2018 04:24
-
-
Save khromov/6c14c69c358ceb272c30 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
Is it still working?
Added to functions.php, couldn't get it to work. My bad, probably. Any ideas?
Used as is 'field' => 'mylink' and 'field' => 'products' (my actual field name)