• Resolved csleh

    (@csleh)


    Using get_pages to list all subpages is working well, but I’d like to style grandchild pages as clearly pages under their parent. Is there a way to add class like “parent” and “child” or something?

    <?php //pull child page content in, remove parent post id to get all sub page levels
    		$mypages = get_pages( array( 
    		'child_of' => $post->ID, 
    		'sort_column' => 'menu_order' ) );
    	  	foreach( $mypages as $page ) {
    	  		$mycontent = $page->post_content;
    		 	$mycontent = apply_filters( 'the_content', $mycontent );
     		?>

    results in all pages having the same styles

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The body class for a child page already declares it a child page and gives you a pointer to its parent. For example,

    <body class="page-template-default page page-id-742 page-child parent-pageid-174 wp-embed-responsive custom-font-enabled single-author">

    So, you could look at the body classes and note that this has two parents, so it’s clearly a grandchild (level 3) page.

    What I’d do is write a filter on body class, look at the classes already assigned, and add “grandchild-page” to the ones that meet the test.

    Thread Starter csleh

    (@csleh)

    thanks but I don’t mean on the child page. Hopefully this will be a better explanation:

    On a grandparent page, use get_pages to get title, etc from all descendants
    I’d like to style like this:

    <div>child 1 page</div>
    <div>child 2 page</div>
     <div class="sub-page">grandchild page of current page (child of child 2 page)</div>
    <div>child 3 page</div>

    right now, all descendants get the same html in the foreach.
    Is there a way to add a class depending on level?
    Or an if is child of a specific page (in this example, child page 2)?
    Or do get_pages for one level deep, then get_pages within that for child pages?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I think you’ll need to check each page to see if it has a child and add your classes as you list them. I’m not seeing a hyper-efficient, simple function call way to get this.

    Thread Starter csleh

    (@csleh)

    I tried this, but each first level child page is showing the “i have kids” text, instead of just the one that actually has children

      <?php $mypages = get_pages( array( 
    	'child_of' => $post->ID, 
    	'sort_column' => 'menu_order',
    	'parent' => $post->ID ) );
    	  foreach( $mypages as $page ) {
    	  	$mycontent = $page->post_content;
    		 $mycontent = apply_filters( 'the_content', $mycontent );
     		?>
     <?php $args = array( 'post_parent' => get_the_ID() );
    	$children = get_children( $args );
    	if ( ! empty($children) ) {  ?>
    		<div>i have kids</div>
    	<?php } else {?>
    		<div>im lonely</div>
    	<?php } ?> 

    can I not use the ID like this? If not, how can I find out if a page has children?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    get_the_ID returns the current pages ID, not the ID of the pages. I think your logic is wrong.

    Thread Starter csleh

    (@csleh)

    my logic is definitely wrong!
    I’ve tried another get pages with child_of post id in various forms, checking if ( is_page() && $post->post_parent )
    and not having any luck.

    how can i test if a page from the first get_pages results has children?

    Moderator bcworkz

    (@bcworkz)

    Short of making a new query, you cannot. Just like in real life, you cannot foresee into the future and know about children not yet “born” (or encountered in a loop). You can only look back from a child’s standpoint to know of their parent.

    Since you will eventually output the child, it’s wasteful to make an extra query. What you could do is buffer the output so you can add in additional classes for the parent once the child status is known.

    Or first loop through the results without output only to learn of child status. Then rewind the posts loop and do the output loop for real. My real life analogy is falling apart, but I think you get the point ??

    Thread Starter csleh

    (@csleh)

    something clicked today and I got it!

    <?php //get first level child pages 
    		$mypages = get_pages( array( 
    		'child_of' => $post->ID, 
    		'sort_column' => 'menu_order',
    		'parent' => $post->ID ) );
    	  	foreach( $mypages as $page ) {
    	  		$mycontent = $page->post_content;
    		 	$mycontent = apply_filters( 'the_content', $mycontent );
     		?>
    <!--if has children, add div in the parent section for each child otherwise no div -->
    <?php
          $childArgs = array(
              'sort_order' => 'ASC',
              'sort_column' => 'menu_order',
              'child_of' => $page->ID
          );
          $childList = get_pages($childArgs);
          foreach ($childList as $child) { ?>
            <div class="grandchild-page">
            <h2><a href="<?php echo get_page_link( $child->ID ); ?>"><?php echo $child->post_title; ?></a></h2>
            </div>  
        <?php } ?><!--end grandchildren-->
    <?php } ?>
    <?php endwhile;  
       endif; ?>	
    • This reply was modified 5 years, 11 months ago by csleh. Reason: resolved
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘target grandchildren (add style?) in get_pages list’ is closed to new replies.