Wordpress首页和帖子页面显示相同的内容
I am working on a basic Wordpress website and wanted the home page to be a custom static page, so I have created a page called home.php
and and page called news.php
and then in the WP admin I've gone to: Setting
then Reading
selected the a static page
option, selected the Front Page
as Home and then the Posts Page
as News.
However when Ive gone to view the site in the browser both pages display the same content.. allthough the URL DOES change to what ever the page is ie: localhost:8888/mysite/home or localhost:8888/mysite/news
I'm not sure if i'm missing something really simple.. but I can't seem to figure out where I am going wrong?
My Code for the home page is:
<?php
/*
Template Name: Home Page
*/
?>
<?php get_header(); ?>
<div class="container">
<h1>This is the homepage</h1>
</div>
<?php get_footer(); ?>
and then my code for the News page is:
<?php
/*
Template Name: News Page
*/
?>
<?php get_header(); ?>
<div class="container">
<h2>This is the Blog page</h2>
<?php $args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 4,
'paged' => get_query_var('paged'),
'post_parent' => $parent
); ?>
<?php query_posts($args); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
<time><?php the_time(get_option('date_format')); ?></time>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
</div>
If anyone could help me try to figure out where I am going wrong, it would be greatly appreciated! :-)
This should probably be moved to the WordPress area.
What are your files named in your theme? WordPress uses a hierarchy to determine which template to use when building a page. You can read more about that in the codex.
For your purposes, it sounds like you need to name your home page template 'front-page.php' and your news page 'home.php'
Note: This is only if you're creating a theme. If you're modifying a theme, its better to give your page templates names like so: 'page-news.php' and 'page-home.php'. See this answer for more details.
Posting with full details.
First, make sure your .php
files are inside the active theme folder.
(eg: /wp-content/themes/<themename>/
).
Create 2 static pages within wordpress first:
Pages -> Add New -> Home
Pages -> Add New -> News
You can enter any title for the page, but make sure they have the slugs home
and news
.
Now, the reading settings. Settings > Reading:
Front page displays: (x) A static page
Front Page: <choose **home** which you created earlier>
Posts Page: <choose **news** which you created earlier>
Now, for the custom homepage, name your php file page-home.php
and for news, page-news.php
. Place them inside your active theme folder.