营销型网站案例,游戏网站制作模板,美食教做网站,沈阳做网站客户多吗大多数WordPress站点首页默认都是显示最新发布的文章列表#xff0c;不过有些站点比较特殊#xff0c;只想显示某一篇文章的全部内容#xff0c;那么应该怎么设置呢#xff1f; 
其实#xff0c;WordPress后台  设置  阅读  在“您的主页显示”中…大多数WordPress站点首页默认都是显示最新发布的文章列表不过有些站点比较特殊只想显示某一篇文章的全部内容那么应该怎么设置呢 
其实WordPress后台  设置  阅读  在“您的主页显示”中选择“一个静态页面在下方选择”的“主页”中选择某一篇文章即可。 不过WordPress后台的“您的主页显示”主页默认只能选择页面不能选择文章页所以需要我们添加以下代码到当前主题的函数文件functions.php实现才行。 
// 阅读设置主页显示可以选择文章
class CustomFrontPage {
private static $instance;
public static function get_instance() {
return isset( self::$instance ) ? self::$instance : new self();
}private function __construct() {
self::$instance  $this;
if ( is_admin() ) {
add_filter( wp_dropdown_pages, array( $this, wp_dropdown_pages ) );
} else {
add_action( pre_get_posts, array( $this, pre_get_posts ) );
add_action( template_redirect, array( $this, template_redirect ) );
}
}public function wp_dropdown_pages( $output ) {
global $pagenow;
if ( ( options-reading.php  $pagenow || customize.php  $pagenow )  preg_match( #page_on_front#, $output ) ) {
$output  $this-posts_dropdown();
}return $output;
}protected function posts_dropdown( $post_type  any ) {
$output  ;
if ( any ! $post_type  ! post_type_exists( $post_type ) ) {
$post_type  page;
}
$posts  get_posts(
array(
posts_per_page  - 1,
orderby  title,
order  ASC,
post_type  $post_type,
post_status  publish,
)
);$front_page_id  get_option( page_on_front );$select  __( Select );
$output . select namepage_on_front idpage_on_front;
$output . option value\0\mdash; {$select} mdash;/option;
foreach ( $posts as $post ) {
$selected  selected( $front_page_id, $post-ID, false );
$post_type_obj  get_post_type_object( $post-post_type );$output . option value\{$post-ID}\{$selected}{$post-post_title} ({$post_type_obj-labels-singular_name})/option;
}
$output . /select;return $output;
}public function pre_get_posts( $query ) {
if ( $query-is_main_query() ) {
$post_type  $query-get( post_type );
$page_id  $query-get( page_id );
if ( empty( $post_type )  ! empty( $page_id ) ) {
$query-set( post_type, get_post_type( $page_id ) );
}
}
}public function template_redirect() {
global $post;
if ( is_singular()  ! is_front_page()  absint( get_option( page_on_front ) )  $post-ID ) {
wp_safe_redirect( site_url(), 301 );
}
}
}CustomFrontPage::get_instance(); 
以上代码来自知更鸟 – WordPress 主页显示设置增加文章选择 
至此我们可以在WordPress后台的“您的主页显示”主页选择文章了但是却显示所有的文章又不能搜索很难找到我们想要的文章这个时候我们可以将上述代码的第32行第40行代码修改为 
$posts  get_posts(
array(
posts_per_page  - 1,
orderby  title,
include  array(6688),
order  ASC,
post_type  $post_type,
post_status  publish,
)
); 
其中第5行代码的6688就是文章的ID如果想要显示多篇文章则在文章ID后面添加英文引号即可如array(6688,6689,6690)。记得修改为自己想要显示的文章ID。 
如果想要显示指定分类的文章也可以将上述代码的第32行第40行代码修改为 
$posts  get_posts(
array(
posts_per_page  - 1,
orderby  title,
category  226,
order  ASC,
post_type  $post_type,
post_status  publish,
)
); 
其中第5行代码的226就是文章分类ID记得修改为想要显示的分类ID即可。 
来源https://boke112.com/post/11751.html