WordPress - 创建页面模板

问题描述:

I converted one static website (HTML + PHP) to WordPress following this guide:https://www.elegantthemes.com/blog/tips-tricks/converting-html-sites-to-wordpress-sites

The Site was structured like this:

**Website Folder
     **css folder
     **images folder
     - index.php
     - head.php
     - header.php
     - footer.php
     - page1.php
     - page2.php
     - page3.php

In the head.php file there was the code inside <head> tag of html, in header.php the <header> of the website (logo and nav menu), in the footer.php the <footer> section. I successfully converted the homepage of the site but the guide doens't say how to make the navigation menu dynamic and also create page templates in order to include page1, page and page3 content. Anyone can help me doing this? Thank you

https://developer.wordpress.org/themes/basics/template-hierarchy/

This article has detailed description of WordPress Template Hierarchy, for example, template name to render a page can in form page-{slug}.php. Also here is an article that explains how to use custom names for page templates, for example custom_page.php, to do that just add opening PHP comment to the file that states the template name.

<?php /* Template Name: Example Template */ ?>

, and go to the Page > Edit screen to select the custom template for the page.

Here is also article that explains how to add navigation menu.

Example ( taken from the link above ):

// functions.php, register menu
function register_my_menu() {
  register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );

// display menu on theme
<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>

// Go to Appearance -> Menus panel to add menu items

If you want page1, page2, page3 in menu. First create a menu in backend and then to create a template use the folllowing line and select for page where you want. Include header file and footer files there. For example:

<?php /* Template Name: Enter your template name */ get_header(); ?><?php while ( have_posts() ) : the_post(); ?>   <?php the_content(); ?><?php endwhile; ?><?php get_footer(); ?>

Now Place the code in header.php file:

<?php wp_nav_menu( array( 'theme_location' => '', 'menu' => 'enter your menu name') ); ?>