黄聪:如何不用插件使用代码调用Wordpress中的随机文章。

最近发现很多收录的文章是显示“重复页面”

下面写出对页面的调整过程

首先删掉了首页的介绍。

原因,所占篇幅过大

减少了随机最新文章和最新评论。

原因,文章和评论更新速度一般。

增加一个随机文章页面

下面这个是代码,供大家参考

<?php
global $wpdb, $tableposts, $randli;
$sql = “SELECT ID, post_title FROM $tableposts WHERE post_status = ‘publish’ “;
$sql .= “AND post_password =” “;
$sql .= “AND post_type <>’page’ “;
$sql .= “ORDER BY RAND() LIMIT 20″;
$randposts = $wpdb->get_results($sql);
$output = ”;
$randli =0″;
foreach ($randposts as $randpost) {
$post_title = stripslashes($randpost->post_title);
$permalink = get_permalink($randpost->ID);
$randli = $randli+1;
$output .=<ul id=”hpost’ . $randli . ‘”><a href=”‘ . $permalink . ‘” rel=”bookmark” title=”‘;
$output .= $post_title . ‘”>. $post_title .</a>’;
$output .=</ul>’;
}
echo $output;
?>

原文地址:https://www.cnblogs.com/huangcong/p/2088799.html