最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - how to get a list of all top level pages

programmeradmin5浏览0评论

I am trying to figure out how to get a list of the permalinks for top-level pages only. My goal is to put them in a selection box on a form. I have spent a couple of hours searching here for an answer to no avail. I was hoping someone could point me in the right direction.

I am trying to figure out how to get a list of the permalinks for top-level pages only. My goal is to put them in a selection box on a form. I have spent a couple of hours searching here for an answer to no avail. I was hoping someone could point me in the right direction.

Share Improve this question asked Aug 8, 2020 at 21:45 petebolducpetebolduc 1294 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1
$query_args = array(
    'post_type'   => 'page',
    'post_status' => 'publish',
    'parent'      => 0,
);

$pages = get_pages( $query_args );

Function get_pages() accepts parameter with parent. Keeping it 0 (zero) will give us first level pages. In the example $pages will contain the first level pages. Use loop for $pages and you can use it in option as you require.

By the sounds of it, this should do what you're after. Just place it in wherever you want your list to output and you should be good to go.

<?php wp_list_pages('depth=1'); ?>

The depth argument being set to 1 is telling the function to only retrieve top level pages, but a lot more arguments are available. Take a look at the dev resources here.

发布评论

评论列表(0)

  1. 暂无评论