WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display child pages, can't find solution (10 posts)

  1. WP-Rasp
    Member
    Posted 1 year ago #

    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 http://codex.wordpress.org/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.

  2. GenericBox
    Member
    Posted 1 year ago #

    Have you closed off the foreach loop after the first </li>?

  3. WP-Rasp
    Member
    Posted 1 year ago #

    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.

  4. WP-Rasp
    Member
    Posted 1 year ago #

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

  5. WP-Rasp
    Member
    Posted 1 year ago #

    Still stuck with one page. Anybody?

  6. GenericBox
    Member
    Posted 1 year ago #

    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.

  7. GenericBox
    Member
    Posted 1 year ago #

    <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>
  8. WP-Rasp
    Member
    Posted 1 year ago #

    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

  9. <article>
  10. 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

  11. <article> </article>
  12. 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

  13. WP-Rasp
    Member
    Posted 1 year ago #

    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 http://codex.wordpress.org/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.

  14. WP-Rasp
    Member
    Posted 1 year ago #

    This is Resolved, thank you @GenericBox

Topic Closed

This topic has been closed to new replies.

About this Topic