wordpress api
There are differences between WordPress posts and pages. There are times when a post is more suitable to use, while for other types of content, a page is more appropriate. In this article, I’ll cover what pages are and why they are different to posts. I’ll then show you how to manage pages on a WordPress website. Finally, we’ll take a look at some functions in the WordPress Pages API and have a play with pages.
WordPress 帖子和页面之间存在差异。 有时候,帖子更适合使用,而对于其他类型的内容,页面更适合。 在本文中,我将介绍什么页面以及为什么页面与帖子不同。 然后,我将向您展示如何管理WordPress网站上的页面。 最后,我们将看一下WordPress Pages API中的一些功能,并对页面进行操作。
什么是WordPress页面? (What Is a WordPress Page?)
A WordPress page is a page of content on your website — for example, a “Contact” or “About” page. We often see links to these pages in the main navigation, sidebar, or the perhaps in the footer of the website.
WordPress页面是您网站上的内容页面,例如“联系”或“关于”页面。 我们经常在主导航,侧边栏或网站的页脚中看到指向这些页面的链接。
The main difference between a post and a page is the context in time: a post is generally time-dependant, while the content within a page is often permanent.
帖子和页面之间的主要区别是时间上下文:帖子通常是时间相关的,而页面中的内容通常是永久性的。
When you publish news, or even a tutorial, the context is important. A new feature won’t be new forever, and in some cases might not exist some years later. That’s why a post is more adapted when writing this type of content.
当您发布新闻甚至教程时,上下文非常重要。 一项新功能不会永远永久存在,在某些情况下,几年后可能不存在。 这就是为什么帖子在编写此类内容时会更适应。
On the other hand, if you want to describe the purpose of your website, or if you want to provide a contact form, you’ll want to use a page. It’s the type of static content that won’t change in time.
另一方面,如果要描述网站的用途,或者要提供联系表格,则需要使用页面。 这是不会随时间变化的静态内容的类型。
如何管理WordPress页面 (How To Manage WordPress Pages)
添加,编辑和删除页面 (Adding, Editing and Removing a Page)
If you have Editor or Administrator permissions, you can manage the pages in WordPress. To do this, go to Pages in the WordPress dashboard, and you’ll see a list of all your pages or to create a new page via the Add New button.
如果您具有“编辑者”或“管理员”权限,则可以在WordPress中管理页面。 为此,请转到 WordPress信息中心中的“页面”,您将看到所有页面的列表,或通过“ 添加新”按钮创建新页面。
![WordPress Pages API](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2017/03/1489682640WordPress-Pages-API-1024x642.png)
This list is similar to the list found in Posts, and can be used in the same way. You can click on a page’s title to edit it and some other useful quick links appear when you move your mouse over the page’s title.
该列表类似于Posts中的列表,并且可以以相同的方式使用。 您可以单击页面标题进行编辑,将鼠标移到页面标题上时,还会显示一些其他有用的快速链接。
When you click to edit or add a page or post, you can then specify the title, content, and even add a featured image if your theme supports it. The Publish button allows you to publish the page. Otherwise, if you’re not ready to make it live, you can save it as a draft.
单击以编辑或添加页面或帖子时,您可以指定标题,内容,甚至在主题支持的情况下添加特色图片。 使用“ 发布”按钮可以发布页面。 否则,如果您还没有准备好将其发布,则可以将其另存为草稿。
整理页面 (Organizing Pages)
To organize your posts, you can use categories and tags. However, there isn’t a similar way to organize pages. Pages can still be organized in a hierarchy. In fact, you can indicate pages as subpages of other pages. It’s exactly like creating subcategories for your categories.
要整理帖子,您可以使用类别和标签。 但是,没有类似的方式来组织页面。 页面仍可以按层次结构进行组织。 实际上,您可以将页面指示为其他页面的子页面。 这就像为您的类别创建子类别。
To create a subpage, go to the Page Attributes box where you’ll find a dropdown list labelled Parent. In this list, select the page you want as the parent of the page you’re currently creating or editing. Note, you can also create sub-subpages, sub-sub-subpages, etc. to create your own hierarchy.
要创建子页面,请转到“ 页面属性”框,您将在其中找到标记为Parent的下拉列表。 在此列表中,选择所需的页面作为当前正在创建或编辑的页面的父页面。 请注意,您还可以创建子页面,子页面等,以创建自己的层次结构。
By default, the pages are sorted alphabetically (by their titles). You can customize this order thanks to the Order field in the Page Attributes section. Then the pages will be sorted following the number you indicate in this field. As an example, if you have three pages titled Page 1, Page 2 and Page 3, they’ll be displayed in this order by default. If you want to make Page 3 appear first, indicate 0
as its order, and bigger numbers for the other pages (1
and 2
for instance).
默认情况下,页面按字母顺序(按标题)排序。 您可以通过“ 页面属性”部分中的“ 订单”字段来自定义该订单。 然后,页面将按照您在此字段中指示的编号进行排序。 例如,如果您有三个页面分别名为Page 1 , Page 2和Page 3 ,则默认情况下将按此顺序显示它们。 如果要使第3页首先出现,请以0
为其顺序,并为其他页面指定较大的数字(例如1
和2
)。
WordPress Pages API (The WordPress Pages API)
显示页面列表 (Displaying a List of Pages)
To display a list of pages, WordPress provides the wp_list_pages()
function. By default, it displays the HTML code for a ul
list containing all the published pages, encapsulated in a li
tag with a h2
heading indicating Pages. It also accepts one parameter: an associative array listing some options to customize the output.
为了显示页面列表,WordPress提供了wp_list_pages()
函数。 默认情况下,它显示包含所有已发布页面的ul
列表HTML代码,这些列表封装在li
标记中,其中h2
标题指示Pages 。 它还接受一个参数:关联数组,列出一些用于自定义输出的选项。
过滤输出 (Filtering the Output)
It’s possible to restrict the number of displayed pages thanks to some options. The first one we will see is child_of
. It allows us to display the pages that have a given page as parent. The following example displays the children of the page with ID 2:
由于某些选项,可以限制显示页面的数量。 我们将看到的第一个是child_of
。 它允许我们显示具有给定页面作为父页面的页面。 以下示例显示ID为2的页面的子代:
<?php
wp_list_pages(array(
'child_of' => 2
));
?>
The authors
option is useful when we want to display pages written by one or more authors. This option admits a string as a value: the list of the authors’ IDs separated by commas. With the following parameter, the function will display the pages written by two authors:
当我们要显示一个或多个作者撰写的页面时, authors
选项非常有用。 此选项允许使用字符串作为值:作者ID列表,以逗号分隔。 使用以下参数,该函数将显示两位作者撰写的页面:
<?php
wp_list_pages(array(
'authors' => '2,7'
));
?>
To display only some pages, you can use the include
option. You give to this option a comma-separated list of pages IDs and you’ll get a list of these pages:
要仅显示某些页面,可以使用include
选项。 您为该选项提供页面ID的逗号分隔列表,您将获得以下页面的列表:
<?php
wp_list_pages(array(
'include' => '7,53,8'
));
?>
On the contrary, if you want to hide some pages, you can use the exclude
option that accepts the same type of value:
相反,如果要隐藏一些页面,则可以使用接受相同类型值的exclude
选项:
<?php
wp_list_pages(array(
'exclude' => '2,4'
));
?>
You can also choose to filter by depth. If you want to see only top-level pages, for instance, you can use the option depth
.
您也可以选择按深度过滤。 例如,如果只想查看顶级页面,则可以使用depth
选项。
By default, it’s set to 0
and all pages are displayed. Set it to any positive number and you’ll get only pages to this depth. For example, indicating 1
will display only the top-level pages. The following example displays these same pages and their direct children:
默认情况下,它设置为0
并显示所有页面。 将其设置为任何正数,您只会得到达到此深度的页面。 例如,指示1
将仅显示顶层页面。 以下示例显示这些相同的页面及其直接子代:
<?php
wp_list_pages(array(
'depth' => 2
));
?>
By default, wp_list_pages()
displays only the published pages. But you can also display other pages, thanks to the option post_status
.
默认情况下, wp_list_pages()
仅显示已发布的页面。 但是由于选项post_status
,您还可以显示其他页面。
Use this option to show the statuses you want to see, separated by a comma. The following example displays the published pages and the drafts:
使用此选项以逗号分隔显示您要查看的状态。 以下示例显示已发布的页面和草稿:
<?php
wp_list_pages(array(
'post_status' => 'publish,draft'
));
?>
In addition to filtering the list of pages you retrieve, you can also limit the number of retrieved pages thanks to the number
option. If you use a positive number, you’ll only retrieve a limited number of pages.
除了过滤检索到的页面列表外,还可以通过number
选项来限制检索到的页面number
。 如果您使用正数,则只会检索有限数量的页面。
自定义输出 (Customizing the Output)
You can customize the heading with the title_li
option that accepts a string as a value. By default, it’s set to Pages, and you can then choose any text to display. You can also set it to an empty string.
您可以使用title_li
选项自定义标题,该选项接受字符串作为值。 默认情况下,它设置为Pages ,然后您可以选择要显示的任何文本。 您也可以将其设置为空字符串。
That way, wp_list_pages()
won’t encapsulate your pages into a li
tag and you’ll directly get the li
tags for the pages.
这样, wp_list_pages()
不会将您的页面封装到li
标签中,您将直接获得页面的li
标签。
检索页面 (Retrieving the Pages)
The wp_list_pages()
function allows you to get a HTML list of pages. If you want to create your own HTML, or if you need to apply some actions to the pages before displaying them, it’s not the best function to use. Instead, you’ll prefer get_pages()
to retrieve an array listing the different pages.
wp_list_pages()
函数允许您获取页面HTML列表。 如果要创建自己HTML,或者需要在显示页面之前对页面应用一些操作,则最好不要使用它。 相反,您get_pages()
喜欢get_pages()
来检索列出不同页面的数组。
This function also accepts an array as an argument, listing the wanted options. The good news is that you already know some of these options, as we covered them in the description of the wp_list_pages()
function: child_of
, authors
, include
, exclude
, post_status
, number
, sort_column
and sort_order
.
此函数还接受数组作为参数,列出所需的选项。 好消息是,您已经知道其中一些选项,因为我们在wp_list_pages()
函数的说明中介绍了这些选项: child_of
, authors
, include
, exclude
, post_status
, number
, sort_column
和sort_order
。
By default, the hierarchy of your pages is shown in get_pages()
: the children will appear right after their parents. You can disable this behavior thanks to the hierarchical
option set to false
. That way, all pages will be treated equally, and the hierarchy will be totally ignored.
默认情况下,页面的层次结构显示在get_pages()
:子级将get_pages()
之后出现。 由于将hierarchical
选项设置为false
,因此可以禁用此行为。 这样,所有页面将被同等对待,并且层次结构将被完全忽略。
检索单页 (Retrieving a Single Page)
The get_pages()
function returns an array listing the wanted pages, each one represented by a WP_Post
object. We’ll now see what’s contained in this object by retrieving only one page. To retrieve a page, use the get_post()
function. Give to this function the ID of a page, and you’ll get an object representing this page:
get_pages()
函数返回一个列出所需页面的数组,每个页面均由WP_Post
对象表示。 现在,我们将只检索一页来查看此对象中包含的内容。 要检索页面,请使用get_post()
函数。 给此函数一个页面的ID,您将获得一个代表该页面的对象:
<?php
$page = get_post(2);
?>
Several pieces of information are available in this object, and each one is an attribute. For instance, the ID of the page can be obtained thanks to the ID
attribute:
该对象中有几条信息可用,每条信息都是一个属性。 例如,由于ID
属性,可以获得页面的ID
:
<?php
echo $page->ID;
?>
The post_content
allows you to retrieve the content of the page, while its title is in the post_title
attribute. The post_name
attribute contains the page’s slug (the part of its URL representing the page’s title).
post_content
允许您检索页面的内容,而页面的标题位于post_title
属性中。 post_name
属性包含页面的post_name
(URL的一部分,代表页面的标题)。
The ID of the page’s author is given with the post_author
attribute. You can also retrieve its date of creation with post_date
and its date of latest modification with post_modified
. The post_status
attribute allows you to know the status of the page (published or draft, for instance).
页面作者的ID由post_author
属性提供。 您还可以检索与创建日期post_date
及其与最新修改的日期post_modified
。 post_status
属性使您可以知道页面的状态(例如,已发布或草稿)。
The ID of the parent page can be obtained with the post_parent
attribute and menu_order
will give you the order indicated during the creation of the page.
可以用获得父页面的ID post_parent
属性和menu_order
会给你创建网页的过程中指示的顺序。
结论 (Conclusion)
If you want to add some static, permanent information to your website, then WordPress Pages are the solution.
如果您想向您的网站添加一些静态的永久性信息,那么WordPress Pages是解决方案。
The functions of the WordPress Pages API are useful if you’re developing a theme. You could, for example, list some pages in the footer, or create a widget to do it, so that your users can list pages wherever they want.
如果您正在开发主题,则WordPress Pages API的功能将非常有用。 例如,您可以在页脚中列出一些页面,或创建一个小部件来执行此操作,以便您的用户可以在所需的任何地方列出页面。
翻译自: https://www.sitepoint.com/wordpress-pages-and-pages-api/
wordpress api