留著備用,之前一直是用外掛實現此功能,總覺得要想讓 WordPress 快一些就不能多用外掛,一是犧
牲了系統穩定性,二是增加了 php 程式碼執行時間,三是有些外掛會發瘋地查詢資料庫,所以能用
程式碼的就堅決不用外掛,不過在模板升級的時候一定要先備份,不然自己都忘了自己做了什麼
修改,當然,你也可以和我一樣,每一次升級或者改動,都做一下筆記。
方法 1 、在 index.php 中查詢 if (have_posts()) 或 while (have_posts()) ,在下面新增:
<!-- If the post is in the category we want to exclude, we simply
pass to the
next post. -->
<?php if (in_category('12') && is_home()) continue; ?>
pass to the
next post. -->
<?php if (in_category('12') && is_home()) continue; ?>
文章 loop 中遇到分類 id 為 12 的文章後立即跳過;如果你設定了首頁顯示 10 篇文章,
但是如果你其他分類下沒有文章,首頁會顯示空白,切記住。
下面的兩種辦法都是採用了 query_posts 函式。
方法 2:轉自露兜部落格,還是在 index.php 中查詢 if (have_posts())
或 while (have_posts()) ,在前面新增 query_posts 函式如下:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
// 不想顯示的分類 ID,多個用半形逗號隔開
'category__not_in' => array(12),
'paged' => $paged
);
query_posts($args);
$args = array(
// 不想顯示的分類 ID,多個用半形逗號隔開
'category__not_in' => array(12),
'paged' => $paged
);
query_posts($args);
方法 3:還是在 index.php 中查詢 if (have_posts()) 或 while (have_posts()) ,
將查詢到的這一整行改成:
if ( have_posts() ) : query_posts($query_string .'&cat=-12'); while
( have_posts() ) : the_post();
( have_posts() ) : the_post();
12 即為不想顯示的分類 ID,多個分類請用半形逗號隔開。