Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using Genesis and have written a custom function which works fine, but is very ugly and includes a css statement. The object is to highlight a menu link in the main header menu when on certain pages or posts.

This function does not work if I remove the css from the function or if I remove "#header #nav #menu-item-133 a" from the stylesheet. It has to have both, so something is not right even though it does do the job!

Can anyone correct my error(s)? Thanks.

function blog_link_genesis() {
    if( is_single() || in_category('investors') || is_page( array(4, 30))) { 
    $current = '#header #nav #menu-item-133 a';  }

    ?>
    <style type="text/css">
    <?php echo $current; ?> { color: #ff0;}
    </style>
    <?php }
add_action( 'genesis_header' , 'blog_link_genesis'  );
share|improve this question
    
This looks to me like you will be echoing malformed CSS most of the time. –  s_ha_dum Aug 25 '13 at 21:47
    
As I said, it works, but please explain your comment? –  Lynne Aug 25 '13 at 21:49
    
You are printing this line-- <?php echo $current; ?> { color: #ff0;}-- every time the function runs, presumably that is on every page load. However, $current is only set for some limited pages so what you are actually printing most of the time is <style type="text/css">{ color: #ff0;}</style>. Note the missing selector. –  s_ha_dum Aug 25 '13 at 21:52
    
Yes, I just saw that it prints on all pages, and highlights only on the pages I need. But I do not know how to write it correctly that is why I am posting the questions here. Thanks for any help. –  Lynne Aug 25 '13 at 21:57
    
I can rewrite that part for you but I don't much about Genesis, so I can't say if that will correct your problem or not. I'd guess "not". –  s_ha_dum Aug 25 '13 at 21:58

1 Answer 1

As noted in comments above, the OP's code generates malformed CSS. Per request, here is corrected code. It should help solve part of the issue (or prevent other issues) but I doubt it solves all of it.

function blog_link_genesis() {
  if( is_single() || in_category('investors') || is_page( array(4, 30))) { 
    $current = '#header #nav #menu-item-133 a'; ?>
    <style type="text/css">
      <?php echo $current; ?> { color: #ff0;}
    </style><?php 
  }
}
add_action( 'genesis_header' , 'blog_link_genesis'  );

All you really needed to do was roll the whole block of code into the if.

I suspect that you have an XY problem in this question. If you were to explain the problem and the desired result better, I suspect that there is a better way to approach this.

If the menu you mean is this one with "Trials", "Cloud Services", "Bright Eval", etc. then that looks like a standard WordPress menu. If you look at the markup, you will notice a number of useful classes. To add more you want to hook into nav_menu_css_class something like what is done here.

share|improve this answer
    
Thank you, the best way to explain this is to show you the messy test site. marazuldelsur.com/wordpress/category/investors In the main top menu I need to have Investors highlighted when on the post category of 'investors' but also on the pages which are in the sidebar menu, 'about' and 'cloud-services'. As the category 'investors' is also the main top link I cannot add the other pages as children. Does that explain it any better. –  Lynne Aug 25 '13 at 22:07
    
It is also like getting single posts to highlight the main menu link to a Blog page when they are not children of 'Blog' –  Lynne Aug 25 '13 at 22:15
    
@Lynne : See the edit. –  s_ha_dum Aug 25 '13 at 22:36
    
Sorry, first time poster so not used to formulating my question the right way. Just read XY question and want to clarify that the desired result was already there. I just thought that my code was clumsy and wanted to get the function to pull a selector from the styles.css file, not have to add it into the function as well. I do not need to add extra classes as I am only targeting one main menu link in the blue bar which does not need to be dynamic. And thank you for your help. –  Lynne Aug 26 '13 at 0:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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