WP高效率的输出分类下文章标题和链接

WordPress中高效率的输出分类下文章标题和链接

//参考:WordPress数据库结构

//非常不建议使用WP_Query,因为WP_Query查询时是将文章所有信息全部查询出来,

//而实际上我们需要的非常少,会非常影响效率,最初我的网站也使用WP_Query,首页打开速度下降明显

//$catID:分类ID
//$postNum:输出文章数量
function outputcatpost($catID,$postNum)
{
global $wpdb, $tableposts;
$request = “SELECT ID, post_title FROM $wpdb->posts WHERE post_status = ‘publish'”;
$request .=” and post_type=’post'”;
//类似于(SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id =156)
$requestIn1=”(SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id =”.strval($catID).”)”;
//(SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id=$requestIn1)
$requestIn1=”(SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN “.$requestIn1.”)”;
$request .=” and ID IN “.$requestIn1;
$request .= ” ORDER BY post_date DESC LIMIT 0, “.strval($postNum);
$posts = $wpdb->get_results($request);
$output = ”;
foreach ($posts as $post)
{
$post_title = stripslashes($post->post_title);
//$permalink = get_permalink($post->ID);
$permalink=”http://www.cadgj.com/?p=”.strval($post->ID);
echo ‘<a href=”‘;
echo $permalink;
echo ‘”>’;
echo $post_title;
echo ‘</a><br/>’;
}
}

此条目发表在网站建设分类目录。将固定链接加入收藏夹。

发表评论