Displaying only immediate children (only one level) pages using get_pages() function


Displaying only immediate children (only one level) pages in WordPress. For example, I have page and sub page structure like:

Page 1
  Page 11
     Page 111
     Page 112
  Page 12
  Page 13
Page 2
  Page 21
  Page 22
     Page 221
     Page 222

Let’s suppose, you need to list all children pages of Page 1, i.e. Page 11Page 12Page 13. But you don’t want to be listed Page 111Page 112 and so on which is WordPress’s default behavior.

Let us assume that ID of Page 1 is 5 then:

$result = get_pages('child_of=5&hierarchical=0&parent=5);

Notice the child_of and parent must have same value. This function will get only three page – Page 11Page 12Page 13 and not Page 111, Page 112 and so on.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.