wordpress 短代码详解

创建流程:

1.创建一个函数,当 WordPress 发现短代码的时候会调用此函数;

function recent_posts_function() {
query_posts(array(‘orderby’ => ‘date’, ‘order’ => ‘DESC’ , ‘showposts’ => 1));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string = ‘<a href=”‘.get_permalink().'”>’.get_the_title().'</a>’;
endwhile;
endif;
wp_reset_query();
return $return_string;
}
2.通过设置一个唯一的名称来注册短代码;

function register_shortcodes(){
add_shortcode(‘recent-posts’, ‘recent_posts_function’);
}
3.把注册的函数绑定到Wordpress的action上。

add_action( ‘init’, ‘register_shortcodes’);