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 badges2 Answers
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.