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'd like to display some CSS only for this page.

In the theme's header.php, I have tried both:

<?php if(is_page('events')) { ?>
<style>
  #main {
    background-image: none;
  }
</style>
<?php } ?>

and

<?php if(is_page(43)) { ?>
<style>
  #main {
    background-image: none;
  }
</style>
<?php } ?>

but these have not succeeded.

I am assuming the slug is "events", and I know the page ID is 43.

Why are these both failing?

Thanks.

share|improve this question
    
What is the WP_Query for this URL? (what is query variables that passed to wp_query on this page - you can get this info from debug bar plugin - WP Query tab)... –  Oleg Butuzov Oct 21 '13 at 12:07
    
are you sure you are targeting the right page? page 43 for instance has the slug 'calendar-of-events'; 'events' seems to be an archive. consider to use the specific body_class() output to style the background. –  Michael Oct 21 '13 at 12:33
    
@Michael I changed the slug to 'calendar-of-events' and the problem remained. Can I use body_class() to style a tag which is not the body tag? –  Steve Oct 21 '13 at 12:52
1  
Steve, please read about "Css Selectors". This is off topic. –  kaiser Oct 21 '13 at 13:01
    
Please edit your question to include the classes being output by body_class(). –  Chip Bennett Oct 21 '13 at 15:09
add comment

3 Answers 3

up vote 0 down vote accepted

instead of echoing your styles anywhere, you can hook your code at wp_head to add it in head or wp_footer to add it in your footer, see if it works for you,

function my_custom_style(){
  global $post;
  if ( !empty ( $post->post_name ) && $post->post_name == 'events' ){ ?>
      <style type="text/css"> 
        #main { background-image: none; }
      </style>
  }

}
add_action ( 'wp_head', 'my_custom_style' );

or

add_action ( 'wp_footer', 'my_custom_style' );

I just saw your page, if you can modify your style then add this to it:

 .events-gridview.events-archive #main {
      background: none;
  }

if you can't then use the

  function my_custom_style(){ ?>
     <style type="text/css"> 
        .events-gridview.events-archive #main {
           background: none;
        }
      </style> <?php
  }

}
add_action ( 'wp_head', 'my_custom_style' );

It would work without conflict.

share|improve this answer
    
Tried this and the code is not being injected. I don't think we're getting the right post_name. The page is generated by a plugin - it is not a Wordpress post or page. –  Steve Nov 4 '13 at 12:22
    
is this code hosted somewhere, so that I can take a look –  sven Nov 4 '13 at 14:25
    
This is functions.php –  Steve Nov 6 '13 at 0:29
    
The CSS .events-gridview.events-archive #main {background: none;} worked. Thank you. –  Steve Mar 10 at 3:11
    
Ha, you remember to reply finally, gr8 to know that it worked :) –  sven Mar 10 at 7:57
add comment

To get post id outside the loop use below code:

<?php 
global $post; 
$id_of_page_or_post=$post->ID;
if($id_of_page_or_post==43){
//do your stuff
} 
?>
share|improve this answer
    
Didn't work either unfortunately @deepa. –  Steve Oct 23 '13 at 0:27
add comment

You can use. get_the_ID() function to getting the id of the page or the post. then write a

$id_of_page_or_post = get_the_ID();
if($id_of_page_or_post==43){
// do something
}

for more reference you can look to the Wordpress Codex of get_the_ID

share|improve this answer
    
Didn't work unfortunately. The URL in question is generated by The Events Calendar plugin. When I am logged in, I do not see 'edit page' in the admin bar for this URL. –  Steve Oct 21 '13 at 12:53
    
Please elaborate your question so I could explain that. I'm not getting what you mean ? You can debug that by using echo or print command –  Kaushik Kishore Oct 21 '13 at 13:27
    
The webpage is not a Wordpress post or page. It is generated by a plugin. –  Steve Oct 23 '13 at 0:30
add comment

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.