Home >CMS Tutorial >WordPress >Understanding WordPress Pages and the Pages API
WordPress Pages and Articles: Static Content Management and API Applications
WordPress pages and articles differ, and they are suitable for different types of website content. This article will explain the purpose of the page and the difference from the article, and explain how to manage pages on a WordPress website. Finally, we will discuss the related functions of the WordPress page API.
Key points:
get_pages()
function can be used to retrieve an array of page lists, while the get_post()
function is used to retrieve a single page. These functions allow further customization and manipulation of the page before it is displayed. What is a WordPress page?
The WordPress page is a content page on a website, such as the Contact Us or About Us page. We often see links to these pages in the main navigation, sidebar, or footer of the website.
The main difference between a page and an article is the relevance of time: the article is usually related to time, while the content of the page is usually permanent.
When publishing a news or tutorial, the time context is very important. New features won't always be new and may not exist in some cases in a few years. This is why it is better to use articles when writing such content.
On the other hand, if you want to describe the purpose of the website, or provide a contact form, you should use the page. This is a static content type that does not change over time.
How to manage WordPress pages?
If you have edit or administrator rights, you can manage pages in WordPress. To do this, go to Pages in the WordPress dashboard and you will see a list of all pages, or you can create a new page with the Add New Page button.
This list is similar to the "Article" list and is used the same way. You can click on the page's title to edit, and when you hover over the page's title, there are some other useful shortcut links that appear.
When you click to edit or add a page or article, you can specify the title and content, and if your theme supports it, you can also add featured images. The Publish button allows you to publish a page. Otherwise, if you are not ready to go live, you can save it as a draft.
To organize articles, categories and tags can be used. However, there is no similar way to organize the page. Pages can still be organized by hierarchy. In fact, you can specify a page as a subpage of another page. It's like creating a subcategory for your category.
To create a child page, go to the Page Properties box, where you will find a drop-down list called Parent Page. In this list, select the page you want to be the parent page that is currently created or edited. Note that you can also create sub-subpages, sub-subpages, etc. to create your own hierarchy.
By default, pages are sorted alphabetically (by title). You can customize this order through the Order field in the Page Properties section. The pages will then be sorted by the number you indicate in this field. For example, if you have three pages titled "Page 1", "Page 2", and "Page 3", they will be displayed in this order by default. If you want "Page 3" to appear first, indicate its order as 0 and the order of other pages as larger numbers (such as 1 and 2).
WordPress Page API
To display a list of pages, WordPress provides the wp_list_pages()
function. By default, it displays HTML code containing an unordered list of all published pages encapsulated in a li tag with an h2 title indicating "page". It also accepts a parameter: an associative array, which lists some options for custom output.
There are some options available to limit the number of pages displayed. The first option we will see is child_of
. It allows us to display pages with a given page as parent page. The following example shows a subpage with page ID 2:
<code class="language-php"><?php wp_list_pages(array( 'child_of' => 2 )); ?></code>
The authors
option is very useful when we want to display a page written by one or more authors. This option accepts strings as values: a comma-separated list of author IDs. Using the following parameters, the function will display a page written by two authors:
<code class="language-php"><?php wp_list_pages(array( 'authors' => '2,7' )); ?></code>
To display only certain pages, use the include
option. You can provide a comma-separated list of page IDs for this option and you will get a list of these pages:
<code class="language-php"><?php wp_list_pages(array( 'include' => '7,53,8' )); ?></code>
In contrast, if you want to hide certain pages, you can use the exclude
option, which accepts the same type of value:
<code class="language-php"><?php wp_list_pages(array( 'exclude' => '2,4' )); ?></code>
You can also choose to filter by depth. For example, if you want to view only the top page, you can use the depth
option.
By default, it is set to 0 and all pages are displayed. Set it to any positive number and you will only get pages of this depth. For example, indicating 1 will only display the top-level page. The following example shows these same pages and their direct subpages:
<code class="language-php"><?php wp_list_pages(array( 'depth' => 2 )); ?></code>
By default, wp_list_pages()
only published pages are displayed. However, you can use the post_status
option to display other pages.
Use this option to display the status you want to see, separated by commas. The following example shows published pages and drafts:
<code class="language-php"><?php wp_list_pages(array( 'child_of' => 2 )); ?></code>
In addition to filtering the list of retrieved pages, you can also use the number
option to limit the number of retrieved pages. If you use a positive number, you will only retrieve a limited number of pages.
You can customize the title using the title_li
option, which accepts strings as values. By default, it is set to Page, and you can then select any text to display. You can also set it to an empty string.
This way, wp_list_pages()
will not encapsulate your page into the li tag, and you will get the li tag of the page directly.
wp_list_pages()
function allows you to get a list of HTML for the page. This function is not the best option if you want to create your own HTML, or if you need to apply certain actions to the page before displaying it. Instead, you prefer to use get_pages()
to retrieve arrays listing different pages.
This function also accepts an array as an argument, listing the required options. The good news is that you already know some of these options, as we have already introduced them in the description of the wp_list_pages()
function: child_of
, authors
, include
, exclude
, post_status
, number
, sort_column
, sort_order
, 🎜> and
get_pages()
By default, the hierarchy of the page is displayed in hierarchical
: the child page appears after its parent page. You can disable this behavior by setting the
The get_pages()
get_post()
function returns an array of required pages, each represented by a WP_Post object. We will now understand what is included in this object by retrieving only one page. To retrieve a page, use the
<code class="language-php"><?php wp_list_pages(array( 'authors' => '2,7' )); ?></code>
This object provides multiple pieces of information, each piece of information being a property. For example, you can use the ID attribute to get the ID of the page:
<code class="language-php"><?php wp_list_pages(array( 'include' => '7,53,8' )); ?></code>
post_content
post_title
allows you to retrieve the content of a page, and its title is in the post_name
property. The
The page author's ID is given by the post_author
attribute. You can also use post_date
to retrieve its creation date and use post_modified
to retrieve its last modified date. The post_status
property allows you to understand the status of the page (such as published or drafted).
The ID of the parent page can be obtained using the post_parent
property, which will give you the order indicated when creating the page. menu_order
If you want to add some static, permanent information to your website, then the WordPress page is the solution.
Functions of the WordPress page API are very useful if you are developing a theme. For example, you can list certain pages in the footer, or create widgets to do this so that your users can list pages wherever they want.
Frequently Asked Questions for WordPress Pages and Pages APIs
WordPress pages and articles are two different types of content. Pages are static and not affected by time, while articles are time-related and used for dynamic content. Pages are often used for content whose content does not change frequently, such as the About Us or Contact Us page. Articles, on the other hand, are used for blogs, news updates, and other content that is regularly updated.
The WordPress REST API provides an endpoint for WordPress data type that allows interaction with your WordPress website over the HTTP protocol. To retrieve page content, you can send a GET request to the /wp/v2/pages
endpoint. This returns a JSON object containing all pages on your website. You can also retrieve specific pages by attaching the page ID to the endpoint, such as /wp/v2/pages/<id></id>
.
To create a new page using the WordPress REST API, you can send a POST request to the /wp/v2/pages
endpoint. The request should contain a JSON object containing the title, content, and status of the page. The status can be "Publish", "Pending", "Draft", or "Private". The API returns a JSON object containing the details of the newly created page.
To update a page using the WordPress REST API, you can send a POST request to the /wp/v2/pages/<id></id>
endpoint, where <id></id>
is the ID of the page you want to update. The request should contain a JSON object containing the updated title, content, or status. The API returns a JSON object with details about the update page.
To delete a page using the WordPress REST API, you can send a DELETE request to the /wp/v2/pages/<id></id>
endpoint, where <id></id>
is the ID of the page you want to delete. The API returns a JSON object with details about the deleted page.
To use the WordPress REST API to retrieve a list of all pages, you can send a GET request to the /wp/v2/pages
endpoint. This returns a JSON object containing a list of all pages on your website.
To use the WordPress REST API to retrieve a list of all published pages, you can send a GET request to the /wp/v2/pages
endpoint and set the "status" parameter to "publish". This returns a JSON object containing a list of all published pages on your website.
To use the WordPress REST API to retrieve a list of all draft pages, you can send a GET request to the /wp/v2/pages
endpoint and set the "status" parameter to "draft". This returns a JSON object containing a list of all draft pages on your website.
To use the WordPress REST API to retrieve a list of all private pages, you can send a GET request to the /wp/v2/pages
endpoint and set the "status" parameter to "private". This returns a JSON object containing a list of all private pages on your website.
To use the WordPress REST API to retrieve a list of all pending pages, you can send a GET request to the /wp/v2/pages
endpoint and set the "status" parameter to "pending". This returns a JSON object containing a list of all pending pages on your website.
The above is the detailed content of Understanding WordPress Pages and the Pages API. For more information, please follow other related articles on the PHP Chinese website!