I want to apply styling only to a page with a specific URL(not id) in Wordpress. for example to .../something/events kindly suggest only PHP solutions, via funtion.php (not CSS id or class) I've tried different things but nothing has worked. thanks.


Why don't you add a class to body via functions if you are on that url, so you can use that class on css, like so:

function add_class_to_body($classes) {
        $classes[] = 'page-events';
        return $classes;

Then you are able to target through

.page-events div{
    background-color: red;
| improve this answer | |
  • Thanks for your solution. I'm sure it will work, but the problem is I have a multilingual website, and there is a problem with a plugin and that I can not translate the slug for some reason (although I have tried so hard to make it happen, and failed). so there's an .../event page and another .../fa/event page, which with your solution there will be no difference if I'm right. The first .../event page works fine. the problem is the second which have a same slug but different url. – Omid Akbari Kh. May 24 at 20:28
  • 1
    I'm sure that if you had added that piece of information to your question that I'd have given a different answer as well. Did you try the code? I'm not sure that even with the translation plugin that the page would be identified – FilT May 25 at 22:28

I found this somewhere out of the blue and it worked! so I share this in case it helps anyone

add_action( 'init', 'apply_css_if_url_contains_string' );

function apply_css_if_url_contains_string() {


if ( false !== strrpos( $url, 'events' ) ) {
echo '<style type="text/css">
     body { font-size: 50px!important }

| improve this answer | |

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.