Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

What causes wp_nav_menu to highlight wrong menu item ? WordPress

  • SOLVED

This little but highly bothering issue is stopping my site from going live. The menu is built as follows:

1st level:
-Main menu ( home / news /studio / projects / contact ) where:
Home: static page (frontage.php)
News: Blog page (index.php)
Studio: static page
Projects: Selection of posts
Contact: static page

2nd level:
-Projects submenu (it displays a range of categories only)

3rd level:
-Submenu for one of the categories above (that also displays categories only)

Now, for some reason, when any menu item under "projects" is clicked, wp thinks it belongs to "news" highlighting it instead of "projects".

The menus are correctly setup in the admin panel so the issue does not come from there I believe. There must be something in the index.php or single.php that it is causing a conflict.

I would email the necessary php files for review and facilitate a user login to the site for testing.



index.php :



<?php get_header(); ?>


<div id="content" class="clearfix half-bottom" role="main">

<?php
$i = 0;

$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('cat=33&posts_per_page=6&order=DES'.'&paged='.$paged);
?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); $i++; ?>

<article <?php post_class() ?>>

<div class="entry g280">
<div class="news-postmetadata add-bottom">
<time datetime="<?php echo date(DATE_W3C); ?>" pubdate class="updated"><?php the_time('d / m / y') ?></time>
</div>

<div class="add-bottom">
<a href="<?php the_permalink() ?>" rel="bookmark" title="" alt="">
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail(array(260,163)); ?>
<?php else : ?>
<img src="<?php bloginfo('template_url'); ?>/images/noimage-260-163.png" width="260" height="163" alt=""/>
<?php endif; ?>
</a>
</div>

<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>

<?php echo apply_filters('the_excerpt',get_the_excerpt().'..| <a href="'.get_permalink().'"><i class="icon-plus"></i></a> |'); ?>

</div>

</article>

<?php if ($i % 3 == 0) echo '<div class="clear"></div>'?>

<?php endwhile; ?>

</div>
<!--end content-->

<div class="posts-nav g840">
<div class="posts-nav-icons">
<div class="prev-posts"><?php previous_posts_link('<i class="icon-chevron-left"></i>') ?></div>
<div class="next-posts"><?php next_posts_link('<i class="icon-chevron-right"></i>') ?></div>
</div>
</div>

<div class="clear"></div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Answers (7)

2012-06-20

Duncan O'Neill answers:

Please send a link to your page. I have a hunch this might be more easily solved with css. Or simply that you need to create a "Projects" post category, and have that menu item contain all posts from that category. Thanks.


binaural comments:

Sent


Duncan O'Neill comments:

Thanks for the link. Have you tried my suggestion above, making a "projects" category for posts, and placing all of the posts shown from the "projects" menu under that category?


binaural comments:

Just tried it. Did not work.


Duncan O'Neill comments:

I saw that. Would you add this to your wp-config.php to find out what is causing the error when the site tries to display a post inth e category 'proyectos'?

define('WP_DEBUG', true);


thanks.


binaural comments:

done


binaural comments:

Actually this error started after trying your first suggestion and reordering the categories back :O


Duncan O'Neill comments:

Yes, because you no longer have the posts in the category 'proyectos'. When they were in that category, it was returning a 404 page. But that 404 page could not be displayed because there was a php error in the 404 template. Try [[LINK href="http://bruto.se/wp/falselink"]][[/LINK]] to see what I mean. The reason it was giving a 404 was because it couldn't find the post in that category. That was just a matter of tweaking your permalinks.

It's difficult to get a solution when things change and then change back without a chance to follow a particular road.

I can better help if you'd send me FTP access details.

best,


Duncan O'Neill comments:

Also, you're now getting this error;


Warning: include(/storage/content/38/101438/bruto.se/public_html/wp/wp-content/themes/2c/proyecto-single.php): failed to open stream: No such file or directory in /storage/content/38/101438/bruto.se/public_html/wp/wp-content/themes/2c/single.php on line 13


Because there is no such file proyecto-single.php in your /themes/2c/ folder.

best


binaural comments:

Yes, I tried renaming that file from "single-proyectos.php" to "proyecto-single.php" to test something quickly and changed it back to its original name. But wp now does not seem to recognize it


Duncan O'Neill comments:

Hi binaural,

this is now fixed. I updated code in functions.php. It's well
commented, so that you can see what's been done. Basically,
I've targeted the two menu items projects and news, and
added or taken away classes according to the page or
category.

best,


binaural comments:

This is great. All working ok now. Thanks Duncan and everyone else for your help on this :)

2012-06-20

Michael Caputo answers:

Is "Projects" a category?


binaural comments:

No. It is a page


Michael Caputo comments:

If your blog links under "projects" are BLOG posts, then it will think they're news - that's why the 'news' menu item is getting that class.


binaural comments:

Please see index.php which is my blog page "news"


Michael Caputo comments:

Is it possible you have a page and a category called Projects?

Fixing with CSS would be easy, but not the most elegant solution


binaural comments:

No. At his point, after hitting my head on the desk for the last 2 days, anything that fixes this will be great :D

2012-06-20

Navjot Singh answers:

Can you paste your index.php here?


binaural comments:

"Projects" links to the projects page, which is a page template that loads 6 posts. Any of those posts link to a single.php that displays the project


Navjot Singh comments:

Thanks. Also I have edited my reply because I think the issue is not with the page Projects use. Can you please paste your index.php here?


binaural comments:

Pasted above. Messy I know


Navjot Singh comments:

Can you give me a temporary login to test?


Navjot Singh comments:

Sorry but I wanted the Wordpress dashboard login not the FTP one.


Navjot Singh comments:

Sorry for the delay in response last night because I slept. But seems you have deactivated the username. Can you please reactivate it? I still haven't checked.

2012-06-20

Daniel Yoen answers:

blog link please :)


binaural comments:

message sent


Daniel Yoen comments:

try this :

function my_page_css_class($css_class, $page) {
if (get_post_type()=='yourpage' || is_page(yourpageid)) {
if ($page->ID == get_option('page_for_posts')) {
foreach ($css_class as $k=>$v) {
if ($v=='current_page_parent') unset($css_class[$k]);
}
}
if ($page->ID==57) {
$css_class[]='current_page_parent';
}
}
return $css_class;
}
add_filter('page_css_class','my_page_css_class',10,2);


hope this help


Daniel Yoen comments:

correction :

function my_page_css_class($css_class, $page) {

if (get_post_type()=='yourpage' || is_page(yourpageid)) {

if ($page->ID == get_option('page_for_posts')) {

foreach ($css_class as $k=>$v) {

if ($v=='current_page_parent') unset($css_class[$k]);

}

}

if ($page->ID==yourpageid) {

$css_class[]='current_page_parent';

}

}

return $css_class;

}

add_filter('page_css_class','my_page_css_class',10,2);


out this code on functions.php


Daniel Yoen comments:

don't forget to replace 'yourpage' and 'yourpageid' with your own :)


binaural comments:

Not working


Daniel Yoen comments:

Projects is Custom Post Type ?


binaural comments:

"Projects" is a main page


Daniel Yoen comments:

try this in functions.php :

add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2 );
function current_type_nav_class($classes, $item) {
$post_type = get_post_type();
if ($item->attr_title != '' && $item->attr_title == $post_type) {
array_push($classes, 'current-menu-item');
};
return $classes;
}


hope this help :)


binaural comments:

No luck either.


Daniel Yoen comments:

try this :

add_filter('wp_nav_menu_objects', function ($items) {
$hasSub = function ($menu_item_id, &$items) {
foreach ($items as $item) {
if ($item->menu_item_parent && $item->menu_item_parent==$menu_item_id) {
return true;
}
}
return false;
};

foreach ($items as &$item) {
if ($hasSub($item->ID, &$items)) {
$item->classes[] = 'current_page_parent';
}
}
return $items;
});


binaural comments:

Daniel, thanks for your effort but that did not work either.

2012-06-20

Romel Apuya answers:

Hi,

Did their suggestions work?
I can help too..
can you provide link to the site and test login to the wp-admin?

cheers,

romel

2012-06-20

Just Me answers:

I doubt it will solve your problem, but there are some semi-colons missing in your code.
Without a link to your site it is a little hard to help.


binaural comments:

Duncan fixed it. Thanks for your help

2012-06-21

Arnav Joy answers:

can you show me url and header.php

if you can provide me access to to your site then please provide me


binaural comments:

All fixed by Duncan. Thanks