!
也想出现在这里? 联系我们
广告位

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

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

  1. // 阅读设置主页显示可以选~ a 3 \ Y p择文章
  2. class CustomFrontPage {
  3. private static $instance;
  4. public static function get_instance() w Q S12\ . F D ` d3;
  5. r= T : u / a F ` neturn isset( self::$instance e w c r ? ! 0 A 3041; ? self::$instance : new self();
  6. }
  7. private function __construct() {
  8. self::$instance = $this;
  9. if ( is_admin() ) {
  10. add_filter( 'wp_dropdown_pages', array( $this, 'wp_dropdown_pages' ) );
  11. } else {
  12. addU & N ) M_action( 'pre_getd T } \ ` # Q z_posts', array( $this, 'pre_get_posts' ) );
  13. add_action( 'template_redit ? \ irect', array( $this, 'templatx , - { } be_redirect' ) );
  14. }
  15. }
  16. public function wp_dropdown_pages( $output ) {
  17. global $pagenow;
  18. if ( ( 'options-reading.php' === $pagenow || 'customize.php' === $pagenow ) &am% ) dp;& preg_matcq Z ^ # 0 /hC e 80; '#page_on_front#', $W % X Z &output ) ) {
  19. $outE K 8put = $this->posts_dropdown();
  20. }
  21. return $q x t ) R 2output;
  22. }
  23. protectF q 6ed function posts_dropdown( $post_type = 'any' ) {
  24. $output =W Y ] } = 5 '';
  25. if ( 'any' !== $post_type && ! post_type_exist } I - m W *ts( $post_type ) ) {
  26. $post_type =S o L V e ] j m c 'page';
  27. }
  28. $posts = get_posts(
  29. array(
  30. 'posts_per_page' => - 1,
  31. 'orderby' => 'title',
  32. 'order' => 'ASC',
  33. 'post_type' => $post_type,
  34. 'pM 1 P f u # Host_status' ={ q O H> 'publish',
  35. &z a 5 $ m#041;
  36. );
  37. $front_page_id = ge0 ( ) J Ot_option( 'page_on_front' );
  38. $select = __( 'Select' );
  39. $output .= '<select name="page_on_front" id="page_on_front">';
  40. $output .= &qup p | W X - |ot;<option value=\"0\"&q \ 1gt;&mdash; {$select} &amp^ 3 J x m a w m;mdash;</option>"v u F;
  41. foreach ( $posts as $post ) {
  42. $selected = selected( $front_page_id, $post->ID, fal$ P c ^ ^ {se k # = G1;;
  43. $post_type_obj = get_post_tk R P 0 ~ v B # Hype_object( $post->post_type );
  44. $output .= "<option value=\"{$post->ID}\"{$selected}>{$post-&gtY ? ^ F u U;post_title} ({$post_type_obj->labels->singular_name})</option>";
  45. }
  46. $ou^ [ 5 rtput .= '</selectr . @ b d r P O>';
  47. return $output;
  48. }
  49. public function pre_get_posts( $query ) {
  50. if 5 z Q k f r040; $query->is_main_query() ) {m E / = T
  51. $post_type = $query->get( 'post_typP 1 1 5e', = t ( S ! ) d q );
  52. $page_y Y 3 d E 0 h W ,id = $query->get( 'paL 9 N 1 p \ $ge_id' );
  53. if ( empty&| m Y 5 S T W#040; $post_tK H h E } ; 5 qype ) && ! empty( $page_id ) ) ) u = ] c y 7 9 03;
  54. $u s x jquery->sey M a | ; ! !t~ } 2 n n0; 'post_type', get_post_type( $page_id ) );
  55. }
  56. m h ! g - z X + `125;
  57. }
  58. public function tempz 7 0 ? 7 M b b 5late_redirect() {
  59. global $post;
  60. if ( is_singular() && ! is_front_page() && absint( get_optionP m ~ t ,040; 'page_on_front' ) ) === $post->ID ) {
  61. wp_safe_redirect( site_url(), 301 );
  62. 4 v t q25;
  63. }
  64. }
  65. CustomFrontPage4 j X E i |::get_instance();

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

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress 后台直接复制文章并设置状态为草稿

2022-5-7 19:55:46

WordPress教程

WordPress 自定义链接插件 Custom Permalinks

2022-5-8 7:55:50

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索