你的shortcode
名称和shotrcode_function_name
是动态的,即随帖子名称的变化而变化,如果两篇帖子的标题相同,则会导致问题。此外,您可能不知道或困惑什么shortcodes
可用。如果你的帖子标题总是唯一的,我建议你这样做。
while ($query->have_posts()) {
$query->the_post();
$shortcodename = strtolower( __to_eng(get_the_title()) );
str_replace(\' \', \'_\', $shortcodename); // your post name, replaced white space with \'_\'
$shortcodefuncname = $shortcodename.\'_shortcode\'; // available shortcodes
$img24x24 = get_the_post_thumbnail_url(\'24x24\');
$attribute = array(\'post_id\' => get_the_ID(), \'post_title\' => get_the_title(), \'image_url\'=>$img24x24, \'post_content\' => get_the_content());
$shortcode[\'attribute\'] = $attribute;
$shortcode[\'func_name\'] = $shortcodefuncname;
$shortcode[\'shortcode_name\'] = $shortcodename;
$shoortcodes[] = $shortcode;
}
wp_reset_query();
foreach ( $shortcodes as $shortcode ){
function $shortcode[\'func_name\']( $atts ){
$atts = shortcode_atts( array( //defaults atts for shortcode
\'image\' => \'show\',
\'content\' => \'show\'
), $atts );
$return = \'<h3>\'.$shortcode[\'attribute\'][\'post_title\'].\'</h3>\';
if( !($atts[\'image\']==\'hide\') ){ // you can set image="hide" in shortcode
$return .= \'<img src="\'.$shortcode[\'attribute\'][\'image_url\'].\'"/>\';
}
if( !($atts[\'content\']==\'hide\') ){
$return .= \'<p>\'.$shortcode[\'attribute\'][\'post_content\'].\'</p>\';
}
return $return;
}
add_shortcode($shortcode[\'shortcode_name\'], $shortcode[\'func_name\']);
}
您的短代码已准备就绪。您可以使用
[my_post_which_i_want] // my_post_which_i_want is post title lowercased and white space replaced with \'_\'