Only show div on homepage/blog index in wordpress/php

I have a wordpress site. I have a div that I only want to appear on the homepage (in the header). What type of php if statement do i need…

In other words:

Read More

only show …. in the header of the main blog index page. Problem is the header itself is loaded in every page.

Thanks!

Related posts

Leave a Reply

4 comments

  1. <?php if(is_home()): ?>
    
    <div>Your div.</div>
    
    <?php endif; ?>
    

    Using above code works fine as long as you are not setting static Page for the Front Page display from here Administration > Settings > Reading.

    <?php if(is_front_page()): ?>
    
    <div>Your div.</div>
    
    <?php endif; ?>
    

    But this code will work irrespective of whether the main blog page is showing or you have set a static page to show on home page.

  2. You could also solve the problem with CSS.

    Set the div to

    #your-div{
    display: none;
    }
    

    in your css.

    Then find the body class page-id for the front page and set the css for that as:

    .body-class-page-id #your-div {
    display: block;
    }
    

    This will hide it on every page except the page you want to show it on.

  3. There’s always $_SERVER['REQUEST_URI'] you can use to determine which page you’re at. For homepage it is usually just ‘/’. so:

    <?php if( $_SERVER['REQUEST_URI'] == '/' ) { ?>
    <div id="home_only">Content goes here..</div>
    <?php } ?>