0

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.

5
  • 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)... Commented Oct 21, 2013 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
    Commented Oct 21, 2013 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
    Commented Oct 21, 2013 at 12:52
  • 1
    Steve, please read about "Css Selectors". This is off topic.
    – kaiser
    Commented Oct 21, 2013 at 13:01
  • Please edit your question to include the classes being output by body_class(). Commented Oct 21, 2013 at 15:09

3 Answers 3

0

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.

5
  • 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
    Commented Nov 4, 2013 at 12:22
  • is this code hosted somewhere, so that I can take a look
    – Kumar
    Commented Nov 4, 2013 at 14:25
  • This is functions.php
    – Steve
    Commented Nov 6, 2013 at 0:29
  • The CSS .events-gridview.events-archive #main {background: none;} worked. Thank you.
    – Steve
    Commented Mar 10, 2014 at 3:11
  • Ha, you remember to reply finally, gr8 to know that it worked :)
    – Kumar
    Commented Mar 10, 2014 at 7:57
2

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
} 
?>
1
  • Didn't work either unfortunately @deepa.
    – Steve
    Commented Oct 23, 2013 at 0:27
1

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

3
  • 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
    Commented Oct 21, 2013 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 Commented Oct 21, 2013 at 13:27
  • The webpage is not a Wordpress post or page. It is generated by a plugin.
    – Steve
    Commented Oct 23, 2013 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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