WordPress教程

WordPress 主题如何设置首页排除某些文章

阿里云

如果你正在运营一个 WordPress 搭建的博客网站,出于某些原因想在博客首页主循环中排除某些文章,该如何实现呢?今天就来分享一下相关的方法。需要注意的是,以下方法只适用于首页是博客文章列表(主循环)的情况。对于某些复杂的门户或杂志布局可能不适用。

简单通过 id 排除文章

如果你要排除的文章数量不多,可以通过下面的函数来实现,直接添加到主题的 functions.php 即可,请根据下面提示修改文章的 id 数组:

也想出现在这里?联系我们
创客主机
  1. /**
  2.  * 博客首页排除某些文章
  3.  */
  4. function wpdx_posts_not_in_home( $query ) {
  5. 	if ( !is_admin() && $query->is_main_query() && is_home() ) {
  6.  
  7. 		$exclude_posts = array( 1,2,18 ); // 根据需要修改数组中的文章id即可,多个ID用英文逗号隔开
  8.         $query->set( 'post__not_in', $exclude_posts );
  9.  
  10. 	}
  11. }
  12. add_action( 'pre_get_posts', 'wpdx_posts_not_in_home', 10, 1 );

我们可以通过 pre_get_posts 钩子来对首页的文章数据做一些过滤。$query->set()可以修改的参数很多,可以参考 wp_query 文档。

更复杂的文章排除方式

最近接了一个小开发,需要在博客首页排除某些文章,而且文章数量很多,使用上面的方法虽然也可以实现,但是每次都要去修改 id 数组,的确不是很方便。所以就使用了另一个方式来实现:在文章编辑界面添加一个选项,勾选后就在博客首页隐藏该文章。

下面就来讲解一下如何实现。
1、在文章编辑界面添加设置选项
在文章编辑界面添加设置选项,使用的是下面的代码,添加到主题的 functions.php 文件即可:

  1. /**
  2.  * 在文章编辑界面添加一个选项
  3.  */
  4. function wpdx_add_a_metabox() {
  5. 	add_meta_box(
  6. 		'wpdx_metabox', // metabox ID
  7. 		__('Display option', 'wpdx-child'), // 选项框标题
  8. 		'wpdx_display_metabox', // 选项的回调函数
  9. 		'post', // 文章类型,多个文章类型请使用array数组
  10. 		'normal', // 显示的位置(normal, side, advanced)
  11. 		'default' // 优先级(default, low, high, core)
  12. 	);
  13. }
  14. add_action( 'add_meta_boxes', 'wpdx_add_a_metabox' );
  15.  
  16. /**
  17.  * 选项的内容
  18.  */
  19. function wpdx_display_metabox( $post ) {
  20. 	/*
  21. 	 * 添加一个安全检查机制
  22. 	 */
  23. 	wp_nonce_field( basename( __FILE__ ), 'wpdx_metabox_nonce' );
  24. 	/*
  25. 	 * 输出选项内容
  26. 	 */
  27. 	$HTML .= '<p><label><input type="checkbox" name="_wpdx_no_home" ';
  28. 	$html .= checked( get_post_meta($post->ID, '_wpdx_no_home',true), 'on', false );
  29. 	$html .= ' />';
  30. 	$html .= __( 'Don\'t show on the homepage', 'wpdx-child');
  31. 	$html .= '</label></p>';
  32. 	/*
  33. 	 * print all of this
  34. 	 */
  35. 	echo $html;
  36. }
  37.  
  38. /**
  39.  * 保存选项的值
  40.  */
  41. function wpdx_save_post_meta( $post_id, $post ) {
  42. 	/* 
  43. 	 * 安全检查
  44. 	 */
  45. 	if ( !isset( $_POST['wpdx_metabox_nonce'] ) 
  46. 	|| !wp_verify_nonce( $_POST['wpdx_metabox_nonce'], basename( __FILE__ ) ) )
  47. 		return $post_id;
  48. 	/* 
  49. 	 * 检查当前用户角色的权限
  50. 	 */
  51. 	$post_type = get_post_type_object( $post->post_type );
  52. 	if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
  53. 		return $post_id;
  54. 	/*
  55. 	 * 自动保存文章时,不保存该选项
  56. 	 */
  57. 	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
  58. 		return $post_id;
  59.  
  60. 	if ($post->post_type == 'post') { // define your own post type here
  61. 		if( !isset($_POST['_wpdx_no_home']) || empty( $_POST['_wpdx_no_home'] ) || $_POST['_wpdx_no_home'] !== 'on' ) $_POST['_wpdx_no_home'] = 'off';
  62. 		update_post_meta($post_id, '_wpdx_no_home', $_POST['_wpdx_no_home']);
  63. 	}
  64. 	return $post_id;
  65. }
  66. add_action( 'save_post', 'wpdx_save_post_meta', 10, 2 );

添加好代码以后,我们就可以看到上图显示的效果。
2、通过钩子过滤首页文章数据
上面代码中,我们添加一个自定义字段 _wpdx_no_home,如果勾选选项,这个字段的值为 on,反之为 off。而对于老文章,就不存在这个字段。

所以,我们同样需要借助 pre_get_posts 钩子来对首页文章数据做一些过滤,仍是添加到主题的 functions.php:

  1. /**
  2.  * 首页排除包含某个字段的文章
  3.  */
  4. function wpdx_home_pre_get_posts( $query ) {
  5. 	if ( !is_admin() && $query->is_main_query() && is_home() ) {
  6.  
  7. 		$meta_query = array(
  8. 			'relation' => 'OR',
  9. 			//老文章不存在_wpdx_no_home字段
  10. 			array(
  11. 				'key' => '_wpdx_no_home',
  12. 				'value' => '',
  13. 				'compare' => 'NOT EXISTS'
  14. 			),
  15. 			//新文章没有勾选选项,_wpdx_no_home 的值为 off
  16. 			array(
  17. 				'key' => '_wpdx_no_home',
  18. 				'value' => 'off',
  19. 				'compare' => '=='
  20. 			)
  21. 		 );
  22. 		$query->set( 'meta_query', $meta_query );
  23. 	}
  24. }
  25. add_action( 'pre_get_posts', 'wpdx_home_pre_get_posts', 10, 1 );

以上的代码就从首页排除了 _wpdx_no_home 值为 on 的文章。这样一来,只要文章勾选了【不要在首页显示】,就可以在首页博客列表中排除了。

WordPress 主题如何设置首页排除某些文章

已有 291 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!