WordPress获取所有页面信息函数:get_pages

WordPress函数get_pages以对象类型返回所有页面信息。

get_pages( array|string $args = array() )

函数参数

$args

数组或字符串

  • child_of:父页面ID,返回指定ID页面的子页面;
  • sort_order:页面的排序方式,可用值:ASC(升序)、DESC(降序),默认值:ASC;
  • sort_column:按字段排序,可用值:post_author、post_date、post_title、post_name、post_modified、menu_order、post_modified_gmt、post_parent、ID、rand、comment*count、 post*;
  • hierarchical:是否以层级方式返回页面,默认值:true;
  • exclude:要排除的页面ID数组;
  • include:指定要返回的页面ID数组;
  • meta_key:返回具有指定Meta Key的页面;
  • meta_value:Meta Key的值;
  • authors:字符串值,页面作者的ID;
  • parent:返回指定页面的子页面;
  • exclude_tree:字符串或数组,排除页面树,包含子页面;
  • number:整数,要返回的页面数量,默认值:0,表示返回所有页面;
  • offset:整数,偏移量;
  • post_type:文章类型;
  • post_status:字符串或数组,页面的状态,例如:publish。

函数返回值

Array
(
    [0] => WP_Post Object
        (
            [ID] => 2
            [post_author] => 1
            [post_date] => 2020-11-27 11:37:03
            [post_date_gmt] => 2020-11-27 03:37:03
            [post_content] => 
            [post_title] => 关于我们
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => about
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2020-11-27 14:45:21
            [post_modified_gmt] => 2020-11-27 06:45:21
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => https://www.beizigen.com/?page_id=2
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    ......
)

函数使用示例

$all_pages = get_pages( array(
	'post_type'         => 'page',
	'post_status'       => array( 'publish' )
) );
foreach($all_pages as $p) {
	echo '<li><a href="' . get_page_link($p->ID) . '">' . $p->post_title . '</a></li>';
}

扩展阅读

get_pages()函数位于:wp-includes/post.php

相关函数:

Typora