Created
August 4, 2014 22:50
-
-
Save Auxo/0e74bceb0834ee50f511 to your computer and use it in GitHub Desktop.
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 | |
// 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