Skip to content

Instantly share code, notes, and snippets.

@Auxo
Created August 4, 2014 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Auxo/0e74bceb0834ee50f511 to your computer and use it in GitHub Desktop.
Save Auxo/0e74bceb0834ee50f511 to your computer and use it in GitHub Desktop.
shortcode
<?php
// ASSUMING [module name="some-slug"]
function cpt_shortcode( $atts ){
$a = shortcode_atts( array(
'name' => 'something else',
), $atts );
if( false === ( $shortcode_post = get_transient( 'shortcode_post' ) ) ) {
$shortcode_post = new WP_Query( array(
'post_type' => 'module',
'name' => $a['name']
));
set_transient( 'shortcode_post', $shortcode_post, 60*60*4 );
}
if ($shortcode_post->have_posts())
while ($shortcode_post->have_posts()):
$shortcode_post->the_post();
// WHICH MODULE
$moduleType = get_field('module_type');
if($moduleType == 'basic'){
include('module-basic.php');
} elseif($moduleType == 'cta'){
include('module-cta.php');
} elseif($moduleType == 'grid'){
include('module-grid.php');
} elseif($moduleType == 'carousel'){
include('module-carousel.php');
}
endwhile;
else
$out = "No modules match your shortcode";
wp_reset_query();
return html_entity_decode($out);
}
add_shortcode( 'module', 'cpt_shortcode' );
// KILL THE SHORTCODE TRANSIENT WHEN A NEW POST IS SAVED
function shortcode_delete_its_transients() {
delete_transient( 'shortcode_post' );
}
add_action( 'save_post', 'shortcode_delete_its_transients' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment