SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
Hybrid View
-
Nov 5, 2008, 16:48 #1
- 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 } ?>
-
Nov 5, 2008, 17:11 #2
- 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
-
Nov 5, 2008, 17:31 #3
- Join Date
- Feb 2007
- Location
- York, PA
- Posts
- 456
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 6, 2008, 00:43 #4
- 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");
Aaron
On Twitter
-
Nov 6, 2008, 08:11 #5
- Join Date
- Feb 2007
- Location
- York, PA
- Posts
- 456
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 6, 2008, 12:34 #6
- 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 } ?>
Aaron
On Twitter
-
Nov 6, 2008, 12:51 #7
- 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
-
Nov 6, 2008, 14:02 #8
- 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");
Aaron
On Twitter
-
Nov 6, 2008, 14:31 #9
- 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.
-
Nov 24, 2008, 08:54 #10
- 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 !
-
Dec 2, 2008, 17:27 #11
- 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
-
Nov 24, 2008, 14:43 #12
- Join Date
- May 2006
- Location
- Aurora, Illinois
- Posts
- 15,476
- 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?Save the Internet - Use Opera | May my mother rest in peace: 1943-2009
Dan Schulz - Design Team Advisor | Follow me on Twitter
SitePoint References: HTML CSS JavaScript | Become A Guru
WordPress SEO Checklist | What WordPress Plugins Do You Use?
Web Standards Curriculum | Image Free Equal Height Columns
-
Nov 24, 2008, 20:16 #13
- 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;
}
Aaron
On Twitter
Bookmarks