V
V
Valery232021-10-22 21:01:09
WordPress
Valery23, 2021-10-22 21:01:09

How to change the html structure of wp_list_pages?

There is a code by which I display a list of child pages (two nesting levels of child pages). Below is the code. Is it possible to somehow change the html structure that is displayed, for example, add ACF fields? Now the structure is like this:

<ul>
<li class="page_item_has_children"><a href="....">Родительская</a>
<ul class="children">
  <li class="page_item page-item-16123 page_item_has_children"><a href="....">Дочерняя</a></li>
  <li class="page_item page-item-16124 page_item_has_children"><a href="....">Дочерняя</a></li>
  <li class="page_item page-item-16219 page_item_has_children"><a href="....">Дочерняя</a></li>
</ul>
</li>
<li class="page_item_has_children"><a href="....">Родительская</a>
<ul class="children">
  <li class="page_item page-item-16130"><a href="....">Дочерняя</a></li>
  <li class="page_item page-item-16132"><a href="....">Дочерняя</a></li>
  <li class="page_item page-item-16133"><a href="....">Дочерняя</a></li>
</ul>
</li>
</ul>


PHP code:
if( is_page() ){
  $pageN = $post->ID;
  if( $post->post_parent ){
    $pageN = $post->post_parent;
  }
  $children = wp_list_pages( 'echo=0&child_of=' . $pageN . '&depth=2&title_li=' );
}
echo $children;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-10-22
@Valery23

Create your request wp_query()and output as you need

// задаем нужные нам критерии выборки данных из БД
$args = array(
  'post_type' => 'page',
  'post_parent' => $pageN
);

$query = new WP_Query( $args );

// Цикл
if ( $query->have_posts() ) {
  echo '<ul>';
  while ( $query->have_posts() ) {
    $query->the_post();
    echo '<li>' . get_the_title() . '</li>';
  }
  echo '</ul>';
} else {
  // Постов не найдено
}
// Возвращаем оригинальные данные поста. Сбрасываем $post.
wp_reset_postdata();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question