0

I have a menu within the sidebar of my site. When you click on the child element of the menu, the li tag adds a current_page_item class to the li tag.

When you land on the page its the parent page and I want the "Overview" li below to add a current_page_item class like its child pages. Does anyone know how to achieve this?

<?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) {
 $parent_title = get_the_title($post->post_parent);
?>

<h3>
Related Pages
</h3>

<ul>
<!-- <li class="current_page_item"> -->
<li>
  <a href="<?php echo get_permalink($post->post_parent) ?>">Overview</a>
 </li>
 <?php echo $children; ?>
 <?php } ?> 
 </ul> 

<div class="sidebar-widget">
 <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('page-sidebar')) ?>
 </div>

Thanks for your help

0

I believe that it will be hard for you to accomplish that without some javascript because you are adding the link manually. However, you can achieve the same thing in another way:

Most wordpress themes come with a body_class() function in the <body> tag. That function adds several classes to the body tag that allows you to better target your content with css. One of the classes added is page-id-* (where * is the page ID). You can read more about body_class here.

So, in your case, the Overview page will also have that page-id-* class that you can use to target your menu.

In you css you can write something like:

.page-id-20 ul.sidebar-menu li:first-child{

  color:green;

}

This assumes a few things:

  1. The Overview page ID is 20. It could be any number, just look at your source code to find out. Or look in the backend to find out the page id.

  2. You have given a class .sidebar-menu to your UL side menu. (Right now, your code doesn't have a class)

  3. The Overview link is always the first link on the list.
  4. The menu is the same on every page.
  • I am glad it worked. – gdaniel Dec 8 '14 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.