Support » Developing with WordPress » Hide a div based on url

  • Resolved matthewpol

    (@matthewpol)


    Hi!
    I am looking for a solution where I can hide the DIV if the user is at a specific url. The content under the URL is dynamically generated.
    Ideally, you could implement your solutions to your functions.php file.

    Thank you in advance!

    Best Regards
    Matthew

    • This topic was modified 1 week ago by matthewpol.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Howdy_McGee

    (@howdy_mcgee)

    Hello,

    This is a difficult question to answer without either seeing the website or knowing the specifics of the URL. In theory, the main <body> tag should have a list of generated classes assigned to it based on the current page. body.home would be your homepage, body.page-123 would be when you’re viewing page with the ID of 123. You could inspect the body tag and see if there’s any special classes you could use in your CSS selector.

    If not, you can use the body_class filter hook to test if you’re on the specific page and push a custom class to the lot:

    /**
     * Add in additional body classes
     * 
     * @param Array $classes
     * 
     * @return Array $classes
     */
    function wpf13546176_body_classes( $classes ) {
    	
    	// If we're viewing page with ID 123
    	if( is_page() && is_page( 123 ) ) {
    		
    		/**
    		 * Will append to the body tag our class
    		 * <body class="... hide-xyz-div">
    		 * 
    		 * Hide our div in CSS
    		 * body.hide-xyz-div div.xyz { display: none; }
    		 */
    		$classes[] = 'hide-xyz-div';
    	}
    	
    	return $classes;
    	
    }
    add_filter( 'body_class', 'wpf13546176_body_classes' );
    matthewpol

    (@matthewpol)

    Hi,
    I used the <body> tag to define which classes I want to exclude. It works! Thank you very much for solving my problem.

    Best Regards
    Matthew

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.