skip to Main Content

I’m trying to show lists of different custom post types (named "leistungen") with corresponding submodules.

So under the URL "/services" I want something like this.

Service A

  • Module A1
  • Module A2
  • Module A3

Service B

  • Module B1
  • Module B2
  • Module B3

I have the following setup in the UI

enter image description here

My current archive loop looks like this. I seem to miss a thing, but I don’t get it after hours of trying.

  <?php
  $outer_args = array(
    'post_type'     => 'leistungen',
    'numberposts'   => -1,
    'post_parent'   => 0
  );

  $outer_query = new WP_Query($args);

  if ($outer_query->have_posts()) : ?>

    <?php while ($outer_query->have_posts()) : $outer_query->the_post(); ?>

      <?php $outer_id = get_the_ID(); ?>
      <h2><?php the_title(); ?></h2>

      <?php $issue = new WP_Query(array(
        'numberposts'   => -1,
        'post_type'  => 'leistungen',
        'post_parent'  => $outer_id,
      )); ?>

      <?php
      if ($inner_query->have_posts()) ?>
      <?php while ($inner_query->have_posts()) : $inner_query->the_post(); ?>
        <p><?php the_title(); ?></p>
      <?php endwhile; ?>
      <?php $inner_query->reset_postdata(); ?>

    <?php endwhile; ?>

  <?php endif; ?>

  <?php wp_reset_query(); ?>

Currently, i neither get results nor any errors.

2

Answers


  1. Chosen as BEST ANSWER

    I had a couple of smaller typos in my code that I just overlooked a couple of times:

    • Misspelled variable
    • missing colon
    • missing closing endif

  2. You could use WP_Query with combination of get_pages to achieve what you want.

    $args = array(
      'post_type'      => 'leistungen',
      'posts_per_page' => -1,
    );
    
    $results = new WP_Query($args);
    
    if ($results) {
      while ($results->have_posts()) {
        $results->the_post();
    
        echo "<h3>" . get_the_title() . "<h3>";
    
        $sub_pages_args = array(
          'post_type' => 'leistungen',
          'child_of'  => get_the_ID(),
        );
    
        $sub_pages = get_pages($sub_pages_args);
    
        echo "<ul>";
        foreach ($sub_pages as $sub_page) {
          echo "<li>";
          echo $sub_page->post_title;
          echo "</li>";
        }
        echo "</ul>";
      }
    }
    
    wp_reset_postdata();
    

    Which outputs this:

    Service A
    
        Module A1
        Module A2
        Module A3
    
    Service B
    
        Module B1
        Module B2
        Module B3
    

    Tested and works! Let me know if you could get it to work too!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search