简体   繁体   中英

Wordpress website with blog page structure

I've built a website (locally) that works just fine and, I've written the home page content in the Index page as I'm accustomed to and, different templates (page1.php, page2.php) for pages that required different layout for the 'About' and 'Contact' pages etc. Since deciding to add a blog though it has me stumped. I've written a blog page and it looks just fine but when I try to view a post I'm just directed back to index.php. I know it's down to the structure but it's confusing. An index page is mandatory, and you should probably have a front-page.php too. If that's the case..which one should I write my home page on? Index page doesn't show in the reading list and choosing "front page' as a static page works but still the blog links go back to index. If I was to put the content from the index page into front-page.php, what would I have in the index page?

Although the Wordpress docs are good I can't really find anything to suggest which way round all this stuff would be.

Your issue is that WordPress by default uses the index.php file for the blog archive (as well as other archives, etc.) and you need to use front-page.php for a dedicated home page with your custom content.

Read https://developer.wordpress.org/themes/basics/template-hierarchy/ on how to construct a standard WordPress theme with template files that follow a standard hierarchy.

The front-page.php template file is used to render your site's front page, whether the front page displays the blog posts index (mentioned above) or a static page. The front page template takes precedence over the blog posts index (home.php) template. If the front-page.php file does not exist, WordPress will either use the home.php or page.php files depending on the setup in Settings → Reading. If neither of those files exist, it will use the index.php file.

Duplicate index.php and call it front-page.php . Edit index.php to be a basic template with a standard loop (and none of your home page content) so that it can function as a blog template that displays the_excerpt or the_content , something like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <h2><?php the_title(); ?></h2>
    <?php the_excerpt(); ?>
<?php endwhile; else: ?>
    <?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); ?>
<?php endif; ?>

See https://developer.wordpress.org/themes/basics/the-loop/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM