我创建了一个小快捷码,允许用户将自定义帖子类型的内容输出到另一个页面:[module name="foo"]
在给定页面上,我有:
[module name="foo"]
[module name="bar"]
然而,输出只是foo的两倍。
相关代码:
<?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\' );
这是
gist.