留着備用,之前一直是用插件實現此功能,總覺得要想讓 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,多個分類請用半角逗號隔開。