• Resolved WP-Rasp

    (@wp-rasp)


    I Appreciate all your help in Advance!

    So I got my theme almost completely cnverted to WP.

    I have 5 Pages that have been created/linked to the Menu.
    Home – Blog – Services- Contact- About Us

    I want to display all the child pages of a page(Services) in Post form.
    I have created a custom Tempate for the page “Services” and I’m working inside that Template.
    My Code looks like this; It’s a thumbnail gallery made with bootstrap.

    <div class="row-fluid">
     <ul class="thumbnails">
     <li class="span4">
     <?php
     $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
       foreach( $mypages as $page ) {
                $content = $page->post_content;
                if ( ! $content ) // Check for empty page continue;
            $content = apply_filters( 'the_content',$content ); ?>
    
    <article class="thumbnail clearfix">
     <img src="img/radiant.jpg">
      <h3><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h3>
      <p><?php echo $content; ?></p>
        <a href="#" class="btn btn-primary pull-right">Read More</a>
                  </article>
                  </li>

    I have about 7 of these <articles> (1 for each child-page)
    I am using https://codex.www.ads-software.com/Function_Reference/get_pages#Displaying_Child_pages_of_the_current_page_in_post_format

    My problem is I can only display one of the child pages of ‘Service’…
    when I insert post_title and the echo &content etc into the second <article> markup, I get the same child page displayed twice in my thumbnail gallery.

    Basically 2 <articles> with the same content and link.

    <article class="thumbnail clearfix">
      <img src="img/radiant.jpg">
     <h3><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h3>
         <p><?php echo $content; ?></p>
         <a href="#" class="btn btn-primary pull-right">Read More</a>
                  </article>
                  </li>
    
     <li class="span4">
     <article class="thumbnail clearfix">
       <img src="img/radiant.jpg">
      <h3><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h3>
        p><?php echo $content; ?></p>
          <a href="#" class="btn btn-primary pull-right">Read More</a>
                  </article>
                  <?php
                     }
                         ?>
        </li>

    I have also tried the query_post with the type of pages but doesnt work. Any help would be much appreciated!

    ps: How do you find out the ID of a page? I’m scared to change my permalink settings bc I heard they can mess up my WP theme if I switch too often.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Have you closed off the foreach loop after the first </li>?

    Thread Starter WP-Rasp

    (@wp-rasp)

    No, I’m sorry I forgot to tell you guys that I have ZERO knowledge of PHP.

    Im pretty good with CSS and HTML.
    I got the display child function working through trial and error.

    is it the <?php endwhile; ?> ?

    If so Do I need to paste <?php endwhile; ?> after every li, and still keep the <?php } ?> at the very end of my thumbnail markup?

    I have 7 child pages and I want to display them all. But for some reason I can only get to display the very 1st one.

    Thread Starter WP-Rasp

    (@wp-rasp)

    I think I’m buying me a PHP book, today.

    Thread Starter WP-Rasp

    (@wp-rasp)

    Still stuck with one page. Anybody?

    I think it is because you are not closing the foreach function.

    foreach is a loop – for each of xx, do this as yy {

    So your function above is saying – for every child page, create an article, with the title/content/read more link.

    But then you haven’t closed the loop – so it isn’t starting again.

    It thinks its still in the one loop.

    Try only this instead:

    Edit: DO NOT PUT the <strong> and <code> tags at the bottom – I was trying to make the Close Loop bold on this post – as it is the important part.`

    Edit 2: I removed the code and put in a new post below as to avoid confusion.

    <div class="row-fluid">
    <ul class="thumbnails">
    
    <?php
    $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
    foreach( $mypages as $page ) {
        $content = $page->post_content;
        if ( ! $content ) // Check for empty page continue;
            $content = apply_filters( 'the_content',$content );
    ?>
    
    <li class="span4">
    <article class="thumbnail clearfix">
      <img src="img/radiant.jpg">
      <h3><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h3>
      <p><?php echo $content; ?></p>
      <a href="#" class="btn btn-primary pull-right">Read More</a>
    </article>
    </li>
    
    <?php
    } // Close the Loop
    ?>
    
    </ul>
    </div>
    Thread Starter WP-Rasp

    (@wp-rasp)

    Thank You @ Generic Box.
    I had that working but I still have my original issue.

    I want to display all of the child pages, this keeps displaying my very first child-page.

    When I duplicate the same

    • <article>

    markup I get the same child page displayed in my second

    <article>

    So basically it keeps on showing my first child page instead of the 2nd child page, and then the 4th, 5th page etc.

    I have so far 7 child pages I want them all displayed in their own

  • <article> </article>
  • This is what I want:

    // Show Tilte & Content of 1st child-page
    <li class="span4">
    <article class="thumbnail clearfix">
      <img src="img/radiant.jpg">
      <h3><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h3>
      <p><?php echo $content; ?></p>
      <a href="#" class="btn btn-primary pull-right">Read More</a>
    </article>
    </li>
    
    // Show Title&Content of 2nd child page
    <li class="span4">
    <article class="thumbnail clearfix">
      <img src="img/radiant.jpg">
      <h3><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h3>
      <p><?php echo $content; ?></p>
      <a href="#" class="btn btn-primary pull-right">Read More</a>
    </article>
    </li>

    Something like that. I have 7 child pages and I need them all in their own thumbnail.
    My static HTML works fine in the template I just cant convert it dynamically, the Title, and the Content etc.

    EDIT: I updated the comments in the code

    Thread Starter WP-Rasp

    (@wp-rasp)

    SOLVED!

    First of all, I was only getting 1 child page because that was the only page that had content in it!

    When I added some sample text inside my child pages, they all showed up!
    Anyway…
    I didnt know that the ‘loop’ basically duplicates your code.

    This is what I did to show my Parent Page as a Title in a header tag; and displays all my child pages of that parent page:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
         <h1 class="text-center"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php endwhile; else: endif; ?>
    
    <div class="row-fluid">
    <ul class="thumbnails">
    <?php
       $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
    
         foreach( $mypages as $page ) {
     	$content = $page->post_content;
        if ( ! $content ) // Check for empty page
     continue;
    
     $content = apply_filters( 'the_content', $content );
                            	?>
    
    <li class="span4">
    
    <article class="thumbnail clearfix">
     <img src="img/radiant.jpg">
       <h3><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h3>
      <p><?php echo $content; ?></p>
             <a href="<?php echo get_page_link( $page->ID ); ?>" class="btn btn-primary pull-right">Read More</a>
    
      </article>
    
          </li>
        <?php
                   	}
                              ?>
    </ul>
    //Then close off whatever HTML tags you have to close

    I wrapped this function around the li https://codex.www.ads-software.com/Function_Reference/get_pages#Displaying_Child_pages_of_the_current_page_in_post_format

    It duplicated each li according to the number of child pages I have created. So every time I add a new child page under “Services” in WordPress, it automatically creates a new li in my code! SWEET!

    By the way. I’m into chapter 3 of my PHP DVD, which just now started talking about the loop.

    Thread Starter WP-Rasp

    (@wp-rasp)

    This is Resolved, thank you @genericbox

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display child pages, can't find solution’ is closed to new replies.