Created
July 12, 2017 04:47
-
-
Save davemac/1672081dd5fa7df496082431076851f6 to your computer and use it in GitHub Desktop.
Uses a WP shortcode to insert ACF pullquotes into content
This file contains hidden or 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
/** | |
* Shortcode to insert ACF dmc_pullquotes repeater field content from current post. | |
* | |
* @param array $atts chooses row to use in the dmc_pullquotes repeater | |
* @param string $content null | |
* | |
* @return string the pullquote content wrapped in a container class | |
*/ | |
function dmc_acf_pullquote_shortcode( $atts, $content = null ) { | |
// Attributes | |
$atts = shortcode_atts( | |
array( | |
'row' => '1', | |
), | |
$atts, | |
'pullquote' | |
); | |
if ( isset( $atts[ 'row' ] ) ) { | |
// user's row index count starts at 1, not 0 | |
$dmc_row = ( $atts[ 'row' ] - 1 ); | |
} else { | |
// if no row attribute, set it to 1 | |
$dmc_row = 1; | |
} | |
// get all the pullquotes repeater fields for the page | |
$rows = get_field( 'dmc_pullquotes' ); | |
// get the chosen pullquote repeater row | |
$dmc_pullquote = $rows[ $dmc_row ]; | |
// get field values from the chosen repeater row | |
$dmc_pullquote_image = $dmc_pullquote['dmc_pullquote_image']; | |
$dmc_pullquote_content = $dmc_pullquote['dmc_pullquote_content']; | |
$output = '<div class="pullquote">'; | |
if ( $dmc_pullquote_image ) { | |
$output .= '<img src="' . $dmc_pullquote_image['url'] . '" alt="' . $dmc_pullquote_image['alt'] . '" />'; | |
}; | |
$output .= wp_kses_post( $dmc_pullquote_content ); | |
$output .= '</div>'; | |
return $output; | |
} | |
add_shortcode( 'pullquote', 'dmc_acf_pullquote_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment