您可以使用WordPress功能wp_get_recent_posts(), 具有必需的参数。
$output = array();
$args = array(
\'numberposts\' => 10,
\'offset\' => 0,
\'category\' => 0,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'include\' => ,
\'exclude\' => ,
\'meta_key\' => ,
\'meta_value\' =>,
\'post_type\' => \'post\',
\'post_status\' => \'draft, publish, future, pending, private\',
\'suppress_filters\' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach( $recent_posts as $post ) {
$output[$post["ID"]][\'title\'] = $post["post_title"];
$output[$post["ID"]][\'title\'] = get_permalink($post["ID"]);
}
add_action(\'wp_head\', \'wpse158654_recent_posts\');
function wpse158654_recent_posts() {?>
<script type="text/javavscript">
var recent_posts = <?php json_encode( $output ); ?>;
</script>
}
或者您可以使用
wp_localize_script() 函数打印所需js的变量。
I haven\'t tested the code, be careful if you are using it on live site directly.