![](/img/trans.png)
[英]Exclude Wordpress posts from being shown on homepage from a specific category
[英]How can I exclude a particular category from my Wordpress homepage?
我在Wordpress網站上有2頁。 所有2頁上均顯示帖子。 我需要限制第一頁中的幾個類別,以及第二頁中的其他幾個類別。
<?php
function excludeCat($query) {
if ( $query->is_home ) {
$query->set('cat', '-3,-5,-23');
}
return $query;
}
add_filter('pre_get_posts', 'excludeCat');
?>
這一項將不起作用,因為它將阻止所有2個頁面中的類別。
我記得在最近的項目中做了這樣的事情,這就是我們的解決方案:
<div class="blog-categories">
<h3>Categories</h3>
<ul>
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list',
'show_count' => 0,
'hide_empty' => 0,
'use_desc_for_title' => false,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '1',
'exclude_tree' => '',
'include' => array( 7, 8, 9, 10, 15 ),
'hierarchical' => 1,
'title_li' => 0,
'show_option_none' => __( '' ),
'number' => null,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => null,
'show_option_all' => 'All',
);
wp_list_categories( $args );
?>
</ul>
</div>
請注意特定類別ID的包含和排除。
您可以在“帖子>類別” WordPress菜單中的某個類別上,然后查看屏幕左下方的鏈接以找到類別ID。
我在最近的項目中應用了類似的方法,這是我們的解決方案:
<div class="blog-categories">
<h3>Categories</h3>
<ul>
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list',
'show_count' => 0,
'hide_empty' => 0,
// 'hide_empty' => 1,
'use_desc_for_title' => false,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '1',
'exclude_tree' => '',
/*
** Note: Hover over a category in 'Posts > Categories' WordPress menu and see link on bottom left of screen to find category ID
*/
'include' => array( 7, 8, 9, 10, 15 ),
// 'include' => '',
'hierarchical' => 1,
'title_li' => 0,
'show_option_none' => __( '' ),
'number' => null,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => null,
'show_option_all' => 'All',
);
wp_list_categories( $args );
?>
</ul>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.