logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$20
What causes wp_nav_menu to highlight wrong menu item ?

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(); ?>

This question has been answered.

binaural | 06/20/12 at 3:45pm Edit

Previous versions of this question: 06/20/12 at 4:04pm

(7) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/20/12
    3:50pm
    Michael Caputo says:

    Is "Projects" a category?

    • 06/20/12 3:53pm

      binaural says:

      No. It is a page

    • 06/20/12 3:58pm

      Michael Caputo says:

      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.

    • 06/20/12 4:06pm

      binaural says:

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

    • 06/20/12 4:26pm

      Michael Caputo says:

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

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

    • 06/20/12 4:31pm

      binaural says:

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

  • avatar
    Last edited:
    06/20/12
    3:56pm
    Navjot Singh says:

    Can you paste your index.php here?

    Previous versions of this answer: 06/20/12 at 3:56pm

    • 06/20/12 3:57pm

      binaural says:

      "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

    • 06/20/12 3:58pm

      Navjot Singh says:

      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?

    • 06/20/12 4:05pm

      binaural says:

      Pasted above. Messy I know

    • 06/20/12 4:32pm

      Navjot Singh says:

      Can you give me a temporary login to test?

    • 06/20/12 5:02pm

      Navjot Singh says:

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

    • 06/20/12 10:48pm

      Navjot Singh says:

      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.

  • avatar
    Last edited:
    06/20/12
    3:53pm
    Daniel Yoen says:

    blog link please :)

    • 06/20/12 3:59pm

      binaural says:

      message sent

    • 06/20/12 4:04pm

      Daniel Yoen says:

      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

    • 06/20/12 4:06pm

      Daniel Yoen says:

      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

    • 06/20/12 4:07pm

      Daniel Yoen says:

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

    • 06/20/12 4:16pm

      binaural says:

      Not working

    • 06/20/12 4:21pm

      Daniel Yoen says:

      Projects is Custom Post Type ?

    • 06/20/12 4:26pm

      binaural says:

      "Projects" is a main page

    • 06/20/12 4:31pm

      Daniel Yoen says:

      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 :)

    • 06/20/12 4:33pm

      binaural says:

      No luck either.

    • 06/20/12 5:52pm

      Daniel Yoen says:

      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;
      });

    • 06/20/12 6:09pm

      binaural says:

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

  • avatar
    Last edited:
    06/20/12
    4:22pm
    Duncan O'Neill says:

    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.

    Previous versions of this answer: 06/20/12 at 4:22pm

    • 06/20/12 4:22pm

      binaural says:

      Sent

    • 06/20/12 4:37pm

      Duncan O'Neill says:

      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?

    • 06/20/12 4:53pm

      binaural says:

      Just tried it. Did not work.

    • 06/20/12 4:58pm

      Duncan O'Neill says:

      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.

    • 06/20/12 5:05pm

      binaural says:

      done

    • 06/20/12 5:07pm

      binaural says:

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

    • 06/20/12 5:25pm

      Duncan O'Neill says:

      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 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,

    • 06/20/12 5:28pm

      Duncan O'Neill says:

      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

    • 06/20/12 5:36pm

      binaural says:

      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

    • 06/20/12 10:57pm

      Duncan O'Neill says:

      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,

    • 06/21/12 1:35am

      binaural says:

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

  • avatar
    Last edited:
    06/20/12
    7:12pm
    Romel Apuya says:

    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

  • avatar
    Last edited:
    06/20/12
    7:21pm
    Just Me says:

    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.

    • 06/21/12 2:53am

      binaural says:

      Duncan fixed it. Thanks for your help

  • avatar
    Last edited:
    06/20/12
    10:28pm
    Arnav Joy says:

    can you show me url and header.php

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

    • 06/21/12 2:50am

      binaural says:

      All fixed by Duncan. Thanks

This question has expired.



Francisco Javier Carazo Gil, Arnav Joy, binaural voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.