I have tried is_front_page() but I believe that is the php way of doing it. Perhaps I'm not calling it right because, of course, I'm not getting a response. Maybe I'm just short of my detailed Js ways of accessing the Wordpress classes.

What I'm trying to do is simple. If I am on the front page or home page add this class if I'm not add this other class. Very simple it's just not working. I have even tried a pseudo span tag and add the class to that and it doesn't work.

  • Have you tried is_home instead - WordPress handles home pages comprised of latest posts vs. static page differently. Also, there are already classes automatically added for the home page via WordPress, so maybe you do not need any more classes than that? – physalis Mar 14 '17 at 10:49
  • Well that may work but what I think I'm trying to do is access a php fiction via jQuery which is the major conflict (I think) – SunRhythms Mar 14 '17 at 10:59
  • Well, honestly, I don’t know what PHP fiction is, but you could possibly use the has_class() function of jQuery to target the home page specifically? Or, if you want to have a script running exclusively on the home page, you could enqueue the script via PHP with the is_home()/is_front_page() condition. – physalis Mar 14 '17 at 11:10
  • cp. api.jquery.com/hasclass – physalis Mar 14 '17 at 11:17

I just posted an answer to another question about how to do it.

In your case, assuming you used body_class() in your theme, your home page should have a <body> with class home to it.

So in your JS, you can:

if( $('body.home').length ){
  // Do stuff

Using jQuery:

    if ( $('body').hasClass('home')) {
    } else {
  • Get by class name isn't always supported and I have had no luck with it ever. – SunRhythms Mar 14 '17 at 12:09
  • Check the updated answer using jQuery. – Dragos Micu Mar 14 '17 at 12:22
  • Cool answer but isn't has class already kind of like a conditional within itself. Do you need the if? I've seen it written without it ijs. When writing jquery to Wordpress you have to change the format. But thnx. It leads me somewhere I will try to write something and hopefully it works. Btw, this is all to fit in an already working and structured jquery document for a plugin that I'm just using a minor condition on because my homepage has a full page background rotator that I need the my header to be on top of in absolute. It only happens a the front page. – SunRhythms Mar 14 '17 at 12:44
  • @SunRhythms As you can see from the docs, hasClass method returns a Boolean, and so you use the IF statement to add a class on True, ELSE a different class on False. api.jquery.com/hasclass – Dragos Micu Mar 14 '17 at 13:32

You can use page id class. If you add page-id class in your java script file maybe solve your problem.

enter image description here

  • Yes I know of the page class. What should I use a bool so if page Id ==== home then such and such? The CSS address is very long even for the area or div which is just the headerwrapper – SunRhythms Mar 14 '17 at 10:54
  • You can use as like if( $('body.page-id-6').length ){ // Do stuff } – ferdouswp Mar 15 '17 at 6:18
  • The checked answer does the trick... its basically the same. thanks – SunRhythms Mar 17 '17 at 2:51

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.