WordPress教程

WordPress 后台主页显示设置增加文章选择

阿里云

WordPress 默认后台阅读设置主页显示只可以选择页面不能选择文章,通过本文的一段代码可以增加选择文章。将代码添加到当前主题函数模板 functions.php 中:

  1. // 阅读设置主页显示可以选择文章
  2. class CustomFrontPage {
  3. 	private static $instance;
  4. 	public static function get_instance() {
  5. 		return isset( self::$instance ) ? self::$instance : new self();
  6. 	}
  7.  
  8. 	private function __construct() {
  9. 		self::$instance = $this;
  10. 		if ( is_admin() ) {
  11. 			add_filter( 'wp_dropdown_pages', array( $this, 'wp_dropdown_pages' ) );
  12. 		} else {
  13. 			add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  14. 			add_action( 'template_redirect', array( $this, 'template_redirect' ) );
  15. 		}
  16. 	}
  17.  
  18. 	public function wp_dropdown_pages( $output ) {
  19. 		global $pagenow;
  20. 		if ( ( 'options-reading.php' === $pagenow || 'customize.php' === $pagenow ) && preg_match( '#page_on_front#', $output ) ) {
  21. 			$output = $this->posts_dropdown();
  22. 		}
  23.  
  24. 		return $output;
  25. 	}
  26.  
  27. 	protected function posts_dropdown( $post_type = 'any' ) {
  28. 		$output = '';
  29. 		if ( 'any' !== $post_type && ! post_type_exists( $post_type ) ) {
  30. 			$post_type = 'page';
  31. 		}
  32. 		$posts = get_posts(
  33. 			array(
  34. 				'posts_per_page' => - 1,
  35. 				'orderby'        => 'title',
  36. 				'order'          => 'ASC',
  37. 				'post_type'      => $post_type,
  38. 				'post_status'    => 'publish',
  39. 			)
  40. 		);
  41.  
  42. 		$front_page_id = get_option( 'page_on_front' );
  43.  
  44. 		$select  = __( 'Select' );
  45. 		$output .= '<select name="page_on_front" id="page_on_front">';
  46. 		$output .= "<option value=\"0\">&mdash; {$select} &mdash;</option>";
  47. 		foreach ( $posts as $post ) {
  48. 			$selected      = selected( $front_page_id, $post->ID, false );
  49. 			$post_type_obj = get_post_type_object( $post->post_type );
  50.  
  51. 			$output .= "<option value=\"{$post->ID}\"{$selected}>{$post->post_title} ({$post_type_obj->labels->singular_name})</option>";
  52. 		}
  53. 		$output .= '</select>';
  54.  
  55. 		return $output;
  56. 	}
  57.  
  58. 	public function pre_get_posts( $query ) {
  59. 		if ( $query->is_main_query() ) {
  60. 			$post_type = $query->get( 'post_type' );
  61. 			$page_id   = $query->get( 'page_id' );
  62. 			if ( empty( $post_type ) && ! empty( $page_id ) ) {
  63. 				$query->set( 'post_type', get_post_type( $page_id ) );
  64. 			}
  65. 		}
  66. 	}
  67.  
  68. 	public function template_redirect() {
  69. 		global $post;
  70. 		if ( is_singular() && ! is_front_page() && absint( get_option( 'page_on_front' ) ) === $post->ID ) {
  71. 			wp_safe_redirect( site_url(), 301 );
  72. 		}
  73. 	}
  74. }
  75.  
  76. CustomFrontPage::get_instance();
也想出现在这里?联系我们
创客主机

之后进入后台 → 阅读设置 → 您的主页显示 → 一个静态页面 → 主页,发现下拉列表中可以选择某篇文章作为首页了。

WordPress 后台主页显示设置增加文章选择

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

收藏
(0)

发表回复

热销模板

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

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