Skip to content

Instantly share code, notes, and snippets.

@badabingbreda
Last active May 15, 2018 21:55
Show Gist options
  • Save badabingbreda/0eb50870b132aff947b8b8804c45de76 to your computer and use it in GitHub Desktop.
Save badabingbreda/0eb50870b132aff947b8b8804c45de76 to your computer and use it in GitHub Desktop.
Shortcode to echo ACF field inline
<?php
/**
* function that will generate the shortcode's output
* @param array $atts
* @return string $output
*/
function func_div_myshortcode( $atts ) {
$atts = shortcode_atts(
array()
, $atts , 'myshortcode' );
$output .= '<div class="myclass">' . get_field( 'myfield' ) . '</div>';
return $output;
}
// add the shortcode so that wordpress knows what to do
add_shortcode( 'myshortcode' , 'func_div_myshortcode' );
// In the texteditor you can add [myshortcode] inline. It will try to match the shortcode it comes across when outputting the page/post. If found it will echo the return-value from the function (func_div_myshortcode) $output. The tricky part it that it will only take a return value, you can not use echo in the function. Well technically you can, but it won't give you the result that you want. You will need the output to a returned string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment