發現一些網路上的資料,許多隨機文章的功能已經不適用現在的WordPress的版本了
所以 iZO提供一個版本給大家囉!
functions.php 新增
//隨機文章
function get_random_posts($posts_num=5,$before='<li>',$after='</li>'){
global $wpdb;
$sqlr = "SELECT ID, post_title,guid FROM $wpdb->posts WHERE post_status = 'publish' AND post_title != '' AND post_password ='' AND post_type = 'post' ORDER BY RAND() LIMIT 0 , $posts_num ";
$randposts = $wpdb->get_results($sqlr);
$outputr = '';
foreach ($randposts as $randpost) {
$post_title = stripslashes($randpost->post_title);
$permalink = get_permalink($randpost->ID);
$outputr .= $before.'<a href="'. $permalink . '">' . $post_title . '</a>';
$outputr .= $after;
}
echo $outputr;
}
$posts_num=5
可以更改顯示筆數
顯示範例:
<ul><li>隨機文章
<ul><?php get_random_posts(); ?></ul>
</li></ul>
