Your shortcode
name and shotrcode_function_name
are dynamic i.e vary with post name, which cause problem if you have same post title for two post. Also, you may not know or may be confused what shortcodes
are available.
I recommend to do this if your post title will always be unique.
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']);
}
Your short code is ready. You can use
[my_post_which_i_want] // my_post_which_i_want is post title lowercased and white space replaced with '_'