Load different div at home template in wordpress

I am working on a custom wordpress homepage.
I want that it is loading in a different div in the header.

The header now loads the #content div for every page. But i want to let it load #contenthome div if the homepage loads.

Read More

I’m using this php-code:

<?php if (is_page_template('template-home.php'))  { ?>
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>

I’m fairly new to php. so i hope you guys can help me 🙂

Thanks a lot.

Related posts

Leave a Reply

3 comments

  1. Or this:

    if(is_home())
    {
        <div id="contenthome">
    <?php } else { ?>
        <div id="content">
    <? } ?>
    

    Personally, I prefer writing things in shorthand when it comes to cases like these. Consider also using this to shorten your code:

    <div id="<?php echo is_home() ? 'contenthome' : 'content'; ?>">
    

    ADDITIONAL NOTES: If your <body> tag has the WordPress body_class() added to it, you can pretty much target individual pages with CSS, even when they’re using the same template. The homepage, like any other page, will have a unique body class applied that will then allow you to target that particular page.

    So if your issue is simply a matter of being able to target any given page regardless of template with CSS, you won’t necessarily NEED to apply any unique divs to your page. For example:

    <style type="text/css">
    #content{display:block;}
    .home #content{display:none;}
    </style>
    

    This will hide the content div only on the homepage. It’s probably not what you want it to do, but you can see how the page itself is being targeted to override the default behavior.

    UPDATE: As per your latest question, if you need another particular page to use that conditional, use is_page() like so:

    LONG CODE:

    if(is_home() || is_page('posts_page_title'))
    {
        <div id="contenthome">
    <?php } else { ?>
        <div id="content">
    <? } ?>
    

    SHORTHAND:

    <div id="<?php echo is_home() || is_page('posts_page_title') ? 'contenthome' : 'content'; ?>">
    

    If you prefer, you may also use is_page(‘posts_page_ID’) or is_page(‘posts_page_slug’). Play around with it and see what works best for you. More information here: http://codex.wordpress.org/Function_Reference/is_page

  2. With reference to this post Try this one….

    $pagename = get_query_var('pagename');
    if ($pagename = 'template-home.php') {
    
    <div id="contenthome">
    
    <?php } else { ?>
    
    <div id="content">
    
    <? } ?>
    
  3. it seems like you already created a file in your theme just for the homepage and you called it template-home.php

    from this point you have two options:

    1) you can just define a constant (or variable) and decide what div to use only when it is defined; if you will use a variable remember that in header.php you are in fact inside a function (get_header) and you will need to use global to have access to it

    2) if you foresee any other differences between the structure of the header between the main page and the rest of your wordpress you could use another file for the header. to do this, in your template-home.php file give a parameter to the get_header function, like get_header(‘home’)

    if you do this header-home.php will be loaded instead of header.php