SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Hybrid View

  1. #1
    Zero Point Zero Siebird's Avatar
    Join Date
    Feb 2007
    Location
    York, PA
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    List parent and subpages even if on subpage in WordPress

    Hi All,

    PHP noob here ...I want to list parent page and subpages even when on a subpage and dynamically highlight the list item of the current page by adding a class "current". Does that make sense? Any thoughts or ideas?

    This currently just list subpages:
    Code PHP:
    <?php
      			if($post->post_parent)
      			$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      			else
      			$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      			if ($children) { ?>
      			<ul class="submenu">
      			<?php echo $children; ?>
      			</ul>
      			<?php } ?>

  2. #2
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your current page should have the class "current_page_item" assigned to it automatically by wp_list_pages. You can use that to style it properly. See:
    http://codex.wordpress.org/wp_list_p..._of_page_items
    Wayne Luke
    ------------


  3. #3
    Zero Point Zero Siebird's Avatar
    Join Date
    Feb 2007
    Location
    York, PA
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hierophant View Post
    Your current page should have the class "current_page_item" assigned to it automatically...
    Wasn't even looking at the source code when testing. That solves that issue, but doesn't solve listing the parent item with the child items (subpages)???

  4. #4
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A simple way to do it would be to add another call to wp_list_pages to your code.
    PHP Code:
    $children wp_list_pages("title_li=&include=".$post->post_parent."&echo=0");
    $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); 

  5. #5
    Zero Point Zero Siebird's Avatar
    Join Date
    Feb 2007
    Location
    York, PA
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaronjj View Post
    A simple way to do it would be to add another call to wp_list_pages to your code.
    PHP Code:
    $children wp_list_pages("title_li=&include=".$post->post_parent."&echo=0");
    $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); 
    Thanks! Not sure where I would add these calls? Do I add another else statement? Bare with me as I'm just learning PHP

  6. #6
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    <?php
    if($post->post_parent){
    	$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); 
    	$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    } else {
    	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    }
    if ($children) { ?>
    	<ul class="submenu">
    		<?php echo $children; ?>
    	</ul>
    <?php } ?>

  7. #7
    Zero Point Zero Siebird's Avatar
    Join Date
    Feb 2007
    Location
    York, PA
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works Perfect! Thanks

    If you don't mind, can explain to me whats happening so I can better understand, and learn. I appreciate all your help

  8. #8
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    <?php
     
    /* if the current pages has a parent, i.e. we are on a subpage */
    if($post->post_parent){
    	$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
    	$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
    } 
     
    /* else if the current page does not have a parent, i.e. this is a top level page */
    else { 
    	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); // form a list of the children of the current page
    }
     
    /* if we ended up with any pages from the queries above */
    if ($children) { ?>
    	<ul class="submenu">
    		<?php echo $children; /*print list of pages*/ ?>
    	</ul>
    <?php } ?>

    You may want to change the else statement to also include the top level page in the list (which would be the current page). In that case you would change $children to be just like the first if statement replacing the instances of $post->post_parent with $post->ID like this
    PHP Code:
        $children wp_list_pages("title_li=&include=".$post->ID."&echo=0");
        
    $children .= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); 

  9. #9
    Zero Point Zero Siebird's Avatar
    Join Date
    Feb 2007
    Location
    York, PA
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I appreciate it! This is my first wordpress theme, slowly I'm learning.

  10. #10
    SitePoint Enthusiast kiwiAMS's Avatar
    Join Date
    Feb 2007
    Location
    Amsterdam
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just wanted to say thank you for this tipp, it helped me greatly too !

  11. #11
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I'm a php/wordpress noob also.
    Could you please tell if I can test if a parent has a particular child, by name or id?
    For example : test if the page "test" has a subpage "test2" or the same thing by id if the page with the id "31" has the page "87" as subpage.
    I was thinking of checking the children list bellow but I didn't manage to do it.
    I also know that a page can be tested by is_page('83') for example, I was wondering if there is something like this for what I want also.
    Thanks

    Quote Originally Posted by aaronjj View Post
    Code PHP:
    <?php
     
    /* if the current pages has a parent, i.e. we are on a subpage */
    if($post->post_parent){
    	$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
    	$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
    } 
     
    /* else if the current page does not have a parent, i.e. this is a top level page */
    else { 
    	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); // form a list of the children of the current page
    }
     
    /* if we ended up with any pages from the queries above */
    if ($children) { ?>
    	<ul class="submenu">
    		<?php echo $children; /*print list of pages*/ ?>
    	</ul>
    <?php } ?>

    You may want to change the else statement to also include the top level page in the list (which would be the current page). In that case you would change $children to be just like the first if statement replacing the instances of $post->post_parent with $post->ID like this
    PHP Code:
        $children wp_list_pages("title_li=&include=".$post->ID."&echo=0");
        
    $children .= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); 

  12. #12
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,478
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick question, Aaron. Is it possible to modify that so it replaces the default "current page" class WordPress generates with something else, like "current-page" perhaps?

    Or would I have to find the function that WordPress uses (which I recall changes with each version) and edit that instead?

  13. #13
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan, it would probably make more sense to setup a filter on wp_list_pages to accomplish that. something like this in your functions.php file in your theme
    PHP Code:
    add_filter('wp_list_pages''change_classes');
    function 
    change_classes($str){
     
    $return str_replace('current_page_item''current-page'$str);
     return 
    $return;

    I didn't test that, but it should get you close. Probably not worth the extra processing in most circumstances though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •