0

I have a slick slider scroller on my home page that cycles through some of the pages I have on my main menu. I am using the following code to do this:

 <div class="page-scroller" >
    <div class="latest-page-ticker">
      <h5>Scroller title</h5>
      <div class="slick-slider">

          <?php 
          global $post;
          $child_pages_query_args = array(
              'post_type'   => 'page',
              'post_parent' => 17,
              'orderby'=> 'title', 
              'order' => 'ASC'
          );

          $child_pages = new WP_Query( $child_pages_query_args );

          while ( $child_pages->have_posts() ) : $child_pages->the_post();

           ?>

            <div class="slide">
              <a class="entry-thumb-wrapper" href="<?php the_permalink(); ?>">
                <div class="entry-thumb">
                  <div class="entry-category">
                    <?php echo get_the_category_list(); ?>
                  </div>
                  <div class="thumb-wrapper">
                    <?php the_post_thumbnail( 'post-small-rectangle-pin' );?>
                  </div>
                </div>
              </a>
              <div class="entry-details">
                <h4 class="entry-title">
                  <a href="<?php the_permalink(); ?>">
                    <?php echo the_title(); ?>
                  </a>                    
                </h4>
                <p>
                  <?php echo side_pages_get_excerpt(); ?>
                </p>
              </div>
              <div class="clearfix"></div>
            </div><!--/ .slide-->
            <?php endwhile; ?>
            <!--/carousel-inner-->
      </div>
    </div>
  </div>

At the moment this scroller cycles through the child pages of the page with the ID 17, which is my first menu item and its children are on this first menu item dropdown menu. How can I have this so that it cycles through all my menu items and not only the child pages of the page with the ID 17, my first menu item? I would like it to cycle through all my menu items in the same way. After cycling through this first item and dropdown menu items it would go to the next item and its child pages(as dropdown items here). Would this be possible to achieve?

3
  • sounds like you can just wrap this in another loop of parent IDs, and instead of setting 17 just use a variable $parent and set this to the parent ID for each menu item at the start of the loop.
    – majick
    Oct 18, 2017 at 15:55
  • Not sure if i quite understand. The $parent would be an array of parent IDs?
    – Paulo
    Oct 19, 2017 at 8:13
  • I meant you could populate $parentids with an array of IDs and then loop with foreach ($parentids as $parent) { (existing code) } so that $parent would be a single ID, but @wunch 's answer is more direct way of doing this.
    – majick
    Oct 20, 2017 at 8:55

1 Answer 1

0

Instead of a single parent page,

'post_parent' => 17,

in the WP_Query params, you can specify an array of parent pages

'post_parent__in' => ['17', '18', '19'],
'orderby' => 'post_parent title',
3
  • As a side note, it would probably be better to grab those post parent id's programmatically, rather than hard-coding them, as post id's can change from site to site.
    – wunch
    Oct 18, 2017 at 16:08
  • Thank you very much! How would I go about getting these programatically?
    – Paulo
    Oct 19, 2017 at 7:47
  • as you mentioned it is menu items, you would probably user wp_get_nav_menu_items and then extract the page IDs to an array developer.wordpress.org/reference/functions/…
    – majick
    Oct 20, 2017 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.