插件
隐藏特定类别 Ultimate Category Excluder
隐藏小部件中某个文章分类 WP Categories Widget
根据用户角色隐藏类别 CaPa Protect
代码
首页中隐藏特定文章类别。functions.php
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
最新文章小工具不显示指定分类的最新文章
wp-includes/widgets/class-wp-widget-recent-posts.php
$r = new WP_Query(
apply_filters(
'widget_posts_args',
array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'cat' => -20
),
$instance
)
);
分类目录小工具不显示指定分类目录
wp-includes/widgets/class-wp-widget-categories.php
$cat_args['title_li'] = '';
$cat_args['exclude'] = '20';
/**
* Filters the arguments for the Categories widget.
*
* @since 2.8.0
* @since 4.9.0 Added the `$instance` parameter.
*
* @param array $cat_args An array of Categories widget options.
* @param array $instance Array of settings for the current widget.
*/
wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
在主页最新文章中隐藏指定分类的文章
index.php
找到while ( have_posts() ) : the_post();
加上if (is_home() && in_category(’20’) ) continue;
wordpress函数
loop循环中获取文章更新时间
<?=the_time(get_option('date_format'))?>
文章内获取分类
<?php $the_post_category = get_the_category(get_the_ID()); echo $the_post_category[0]->cat_name; ?>
文章摘要
文章地址
文章标题
max(1, get_query_var('paged')); //当前第几页
get_template_part( 'content',''); //文章内容
页面底部区域
顶部导航栏
<?php
//文章查询分类id=20,每页5篇文章,定位到第二页,标签是wordpress
query_posts('cat=2&posts_per_page=5&paged=2&tag=wordpress);
while(have_posts()): the_post();
echo ''.the_title().'';
endwhile;
wp_reset_query();
end.