Now leave index.php file with static content as it is. Let’s start creating page.php file which is responsible to layout of individual pages.
Use get_header() function to load necessary JS & CSS files in header. Now our intention is to load dynamic page content from database as well as to load dynamic page meta data, page title, breadcrumbs etc. For that we use WordPress functions here.
Create a while loop which loads the page title, page content.
while(have_posts()) {
the_post();
the_title();
the_content();
}
At this stage we create a div where we list out the child page items & for that we use some WordPress functions.
get_pages() | Retrieves an array of pages |
the_ID() | Displays the ID of the current item in the WordPress Loop. |
get_the_ID() | Retrieves the ID of the current item in the WordPress Loop |
wp_get_post_parent_id() | Returns the ID of the post’s parent. |
get_permalink() | Retrieves the full permalink for the current post or post ID. Id should be passed. |
the_permalink() | Displays the permalink for the current post. |
get_the_title() | Retrieves the post title. Id should be passed. |
the_title() | Displays or retrieves the current post title with optional markup. |
wp_list_pages() | Retrieves or displays a list of pages (or hierarchical post type items) in list (li) format. |
Please note similar type of functions, get_ID() & the_ID(), the_title() & get_the_title(),
Here the functions with the_ will print/echo the output & also it points to current post item in loop while the functions with get_ on will NOT print/echo any output & ID should be passed as an argument here. This output can be used to store into any variable that’s why we are not directly printing the output.
That’s why we use the_title(), the_content(), the_permalink() into while loop while creating a basic page layout. This functions will print the output. We are NOT using echo the_title(). We just use the_title() and it displays the current post title in the loop. While when we have a post id different from the current post id in the loop and we want to print the title of that post we should use echo get_the_title(ID). I hope that’s clear.
So making it easy, Just use the_ when you want to work with current post item data in the loop OR if you have different post in the loop and you want the data of that posts, just use get_