Website designs and more
Services
About
Portfolio
Contact

How to add a CSS class based on a query string in WordPress

Nov 26, 2020 | Web Developer Tips | 0 comments

The code below is for a query string named “theyear”. It adds the class “customHide” if the query string contains “2019”. If not, it adds the class “customHideTwo”.

For example, if your site URL contains this query string:
https://www.yoursite.com?theyear=2019

Add the following PHP code to your child theme functions.php file.

[php]
// look for parameter in URL for year and add the class customHide
if (isset($_GET['theyear'])) {
  $theyear = $_GET['theyear'];
    if ($theyear = '2019') {
  // Add specific CSS class by filter. 
    add_filter( 'body_class', function( $classes ) {
        return array_merge( $classes, array( 'customHide' ) );
    } );
    }
} else {
  //Handle the case where there is no parameter
  // Add the class customHideTwo
    add_filter( 'body_class', function( $classes ) {
        return array_merge( $classes, array( 'customHideTwo' ) );
    } );
}
[/php]

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.