A Complete Guide To WordPress Pages

Pages are similar to posts in WordPress, because it is stored in wp_posts table as well. But, it has different chronological order when we compare it to the posts because we can also created sub-pages of a page. Posts are used for writing normal blog which appears in chronological order on index.php, category.php etc. We mostly use Pages for “Contact Us”, “About” etc purposes. But we can also use Pages for looping posts by using get_posts(), query_posts() and WP-Query.

 

How to Create a Page in WordPress

[code type=”codetype”]
<?php
/* Template Name: My Page *?
?>
[/code]

Now I’ll tell you about the different hooks regarding Pages.

1. get_all_page_ids()

This hook retrieves all the page IDs.

Syntax

[code type=”codetype”]<?php get_all_page_ids(); ?>[/code]

2. get_page()

It allows you to create static content that will not make things change on archives, articles or blog. By using this hook, you can easily turn your WordPress blog into a static website. The page id parameter is required.

Syntax

[code type=”codetype”]
<?php get_page($your_page_id); ?>
[/code]

Example

[code type=”codetype”]
<?php
$pageId = 4;

$get_page_data = get_page($pageId);
//Echoing Title
echo ‘<h1>’. $get_page_data->post_title . ‘</h1>’;
echo $get_page_data->post_content;
?>
[/code]

3. get_ancestors

get_ancestors function returns an array containing the parents of page, category etc. For example, you have a page hierarchy like Entertainment (28) -> Child Page (23). (28) specifies the page’s id. Now you can call ancestors of child page by using the following code.

[code type=”php”]
<?php
get_ancestors(23, ‘page’); //If you are working with categories, specify ‘category’ in second parameter
?>
?>
[/code]

Output

Array
{
[0] => 28
}

4. get_page_link

get_page_link returns permalink of a page whether it is used in a loop or outside loop. If you are using inside loop you will have to use query_posts() first like below.

[code type=”php”]
<?php
query_posts(array(‘post_type’ => ‘page’));
if(have_posts()) : while(have_posts()) : the_post();
echo ‘<a href=”‘.get_page_link($post->ID). ‘”>Read More</a>’;
endwhile;
endif;
wp_reset_query();
?>
[/code]

Otherwise, you can use this function like below

[code type=”php”]

<a href=”<?php echo get_page_link(38); ?>”>Read More</a>
[/code]

5. get_page_by_title

If you want to get a page by title, this WordPress function does that. You can get the page by title like below

[code type=”php”]
<?php
get_page_by_title(‘Contact’);
?>
[/code]

6. get_page_children

I’ve already mentioned get_ancestors above where you can retrieve child page’s parents. By using get_page_children, you can retrieve parent page’s children pages.

Example:

[code type=”php”]
<?php

$ini_query = new WP_Query();
$getting_all_wp_pages = $ini_query->query(array(‘post_type’ => ‘page’));

$get_portfolio_page =  get_page_by_title(‘Portfolio’);

// Search through all pages and find children pages
$portfolio_page_children = get_page_children( $get_portfolio_page->ID, $getting_all_wp_pages );

// echo
echo ‘<b>’ . print_r( $portfolio_page_children, true ) . ‘</b>’;
?>
[/code]

7. get_page_uri

Able to get uri of any page as a permalink.

Example:

[code type=”php”]
<?php
$pageID = 10;
$pageURI = get_page_uri($pageID);
echo ‘<a href=”‘. $pageURI .'”>Test Page</a>’;
?>
[/code]

8. is_page

This is a conditional tag for page that checks if pages are being displayed or not and returns the result as boolean (true or false). It doesn’t work inside loop.

[code type=”php”]
is_page(23);           //Page with id number 23 is being displayed
is_page(‘Contact Page’); //Page with “Contact Page” name being displayed
is_page(‘hire-me’);      //Page with slug “hire-me” will display
[/code]

9. wp_link_pages

This function is used for pagination and it must be used inside the loop.

[code type=”php”]
<?php
wp_link_pages(‘before=<div>&after=</div>&next_or_number=number&pagelink=page %’);
?>
[/code]

10. wp_list_pages

This function lists all the pages in your WordPress blog. You can use this function anywhere in the template.

[code type=”php”]
<?php
$args = array(
‘post_status’ =>  ‘publish’,
‘post_type’   =>  ‘page’,
‘walker’      =>   ”,
‘link_after’  =>   ”,
‘link_before  =>   ”,
‘sort_column’ =>   ‘menu_order, post_title’,
‘authors’     =>   ”,
‘echo’        =>    1,
‘title_li’    =>   __(‘Pages’),
‘include’     =>   ”,
‘exclude’     =>   ”,
‘child_of’    =>    0,
‘date_format’ =>   get_option(‘date_format’),
‘show_date’   =>   ”,
‘depth’       =>   0
);
?>
[/code]

11. wp_page_menu

If you want to display your site’s menu as pages then this function will help you.

[code type=”php”]
<?php
wp_page_menu(array(‘sort_column’ => ‘menu_order’, ‘show_home’  => ‘Blog’));
?>
[/code]

Duan Lingxin

Duan Lingxin

Content crafter and chief editor at Scratching Info. Also regular contributor on other major online tech platforms. Security Specialist by day and a writer by night, he does his best to instill his knowledge about tech while delivering inspiring and life changing resources through his writing,

More Posts - Website

Leave a Comment