WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_pages - Show grandchildren pages but exclude parent (5 posts)

  1. aliceralph
    Member
    Posted 1 year ago #

    Hi

    I am wondering if this is possible. Basically my pages are set up like this:

    Projects (main parent page)

    - Category 1 (sub-parent page)
    -- Project 1 (grandchild page)

    - Category 2
    -- Project 2
    -- Project 3

    - Category 3
    -- Project 4
    -- Project 5
    -- Project 6

    ...etc

    I want to show all pages within Projects (ID=2) but exclude the Category pages. ie. only show all of the grandchildren / 'projects'.

    My current code is:

    <?php
    			$mypages = get_pages( array( 'child_of' => '2', '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 );
    			?>
    
    		<h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?><?php echo $page->post_title; ?></a>
    		</h2>
    
    			<?php
    			}
    		?>

    ...but this shows the Category pages as well. Any way that I can exclude these pages?

    Thanks

    Alice

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this [untested]:

    <?php
    $mypages = get_pages( array( 'child_of' => 2, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
    
    			foreach( $mypages as $page ) {
    
    			if($page->post_parent != 2){
    				$content = $page->post_content;
    				if ( ! $content ) // Check for empty page
    					continue;
    
    				$content = apply_filters( 'the_content', $content );
    			?>
    
    		<h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?><?php echo $page->post_title; ?></a>
    		</h2>
    
    			<?php
    			}
    			}
    		?>
  3. aliceralph
    Member
    Posted 1 year ago #

    Hey keesiemeijer, this worked perfectly!! Thanks so much!

    So I'm guessing it's the post_parent bit that does the trick?

    This was so helpful, thanks for taking the time to help. Really appreciate it.

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved :-)

    So I'm guessing it's the post_parent bit that does the trick?

    Correct.

  5. Rains
    Member
    Posted 1 year ago #

    Hello

    I want to show the child pages as tabbed content and each of the child page may or may not have sub-child Pages or if i should say grand child page of the Parent Page. I want a loop or a wp query that can show these sub-pages of the child Pages to show their thumbnails or image fetched through a custom field to show in a row and when user clicks on these images it opens the content of that particular sub-Page
    in a lightbox or fancybox.
    So i have a code that shows these child pages as tabs. So if there's a

    Parent Page
    - Child A
    -- Grand Child A
    -- Grand Child B

    - Child B
    -- Grand Child 1
    -- Grand Child 2

    All these child Pages are showing on the Parent Page as Tabs. I want if the tab content for Child A is showing which is default the grand child pages of child A - means Grand Child A, Grand Child B ... should display as a row showing the images placed using a custom field. So when a user clicks that thumbnail i want this Grand Child A content to open in lightbox or fancybox. And if user clicks Child B tab then the grand child pages show - , Grand Child 2, ... and on click of the images for Grand Child 1 or 2 its respective content should open in a lightbox or fancy box.

    The code that i am using to show the child Pages on Parent Page as tabs is :

    <ul class="nav nav-tabs" id="myTab">
    <?php
    $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&hierarchical=0&sort_order=desc&parent='.$post->ID);
    $count = 0;
    foreach($pages as $page)
    {
        $content = $page->post_content;
        if(!$content)
            continue;
        if($count >= 100)
            break;
        $count++;
        $content = apply_filters('the_content', $content);
    ?>
    
    <li>
    <a>"><?php echo $page->post_title ?></a>
    </li>
    <?php } ?>
    
    <div class="tab-content">
    
    <?php
        $pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&hierarchical=0&sort_order=desc&parent='.$post->ID);
        $count = 0;
        foreach($pages as $page)
        {
            $content = $page->post_content;
            if(!$content)
                continue;
            if($count >= 100)
                break;
            $count++;
            $content = apply_filters('the_content', $content);
        ?>
    
    <div class="tab-pane" id="<?php echo $count ?>">
    <?php echo $content ?>
    </div>
    
    <?php } ?>
    
    </div>

    [Please post code or markup snippets between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    Regards

Topic Closed

This topic has been closed to new replies.

About this Topic