WordPress函數(shù) query_posts的基本用法
在主題目錄下找到存檔頁(yè)面文件,存檔頁(yè)面包括index.php、archive.php等,一般分類頁(yè)、標(biāo)簽頁(yè)、日期頁(yè)和作者頁(yè)等都是用archive.php。
確定了你要控制哪個(gè)頁(yè)面的文章列表,那么我們就可以開(kāi)始了,比如你想讓首頁(yè)的文章按評(píng)論數(shù)排序,那么index.php中的代碼基本框架就是這樣的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | <?php // query_posts函數(shù)query_posts('orderby=comment_count'); // 主循環(huán)if ( have_posts() ) : while ( have_posts() ) : the_post();
..endwhile; else:
..endif; // 重置querywp_reset_query(); ?> |
其實(shí)你要做的就是在index.php中查找if (have_posts())
或while (have_posts())
,在前面添加query_posts函數(shù)即可。不過(guò)以上方式可能會(huì)導(dǎo)致首頁(yè)無(wú)法分頁(yè),那你可以將query_posts函數(shù)改成這樣的行式:
1
2
3
4
5
6
7
8 | // 下面這一行代碼是必須的,不然你的首頁(yè)不能分頁(yè)$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;$args = array(
// 這里以下面的方式添加query_posts參數(shù),具體參數(shù)可以參加官方文檔
'orderby' => comment_count,
'paged' => $paged);query_posts($args);
|
會(huì)員登錄
賬號(hào)登錄還沒(méi)有賬號(hào)?立即注冊(cè)