• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

add a body class on any url with a particular word in slug

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › add a body class on any url with a particular word in slug

This topic is: resolved

Tagged: body_class, strpos($_SERVER['REQUEST_URI']

  • This topic has 2 replies, 2 voices, and was last updated 8 years, 8 months ago by Bill.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • March 19, 2018 at 7:15 am #218094
    Bill
    Participant

    I am really close but need some help with this. I want to add a body class to pages that have a particular slug word. I already have functioning code to accomplish the part where I target the slug word (in this example, the url slug word would be "myslug". for instance, this works for forcing a full width page only on pages with this slug word in the url:

    function mcq_new_cpt_layout() { 
      if ( strpos( $_SERVER['REQUEST_URI'], "myslug" ) ) {
    		return 'full-width-content';
    	}
    }
    
    add_filter( 'genesis_site_layout', 'my_new_cpt_layout' ); 

    so, now I also want to create a functions code to add a body class to pages that have this slug word in URL. This is what I tried:

    function add_my_page_body_class( $classes ) {
    	  if ( strpos( $_SERVER['REQUEST_URI'], "myslug" ) ) {
    	  $classes[] = 'my-new-body-class';
    	  return $classes;
    	
    	}
    }
    
    add_filter( 'body_class', 'add_my_page_body_class' );

    THe problem is it does successfully add the "my-new-body-class" to pages with the "myslug" in the url, however, I get a white screen on any OTHER page without it. what am I missing?

    March 19, 2018 at 7:39 am #218096
    Brad Dalton
    Participant

    This code might work.


    Tutorials for StudioPress Themes.

    March 19, 2018 at 7:59 am #218099
    Bill
    Participant

    Yep, upon a quick test, that seems to have worked Brad!

    I will test a bit further. Thanks so much!!!
    Bill

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘add a body class on any url with a particular word in slug’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble