Forums

[resolved] if is_front_page not working (9 posts)

  1. sp00f
    Member
    Posted 2 years ago #

    im trying to add to my index.php a code that if its the front page of the site, it will do something, and if its not(regular page) it will do something else.

    iv'e tried this code`
    <?php
    if ( is_front_page() ) {
    blabla home
    } else {
    bla bla data
    }
    ?>
    `

    but its not working ! i read every post about this issue and still no luck :(

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    From Conditional Tags

    The Main Page

    is_home()
    When the main blog page is being displayed. (WordPress 2.1 handles this function differently than prior versions. See Alternate Methods for Setting the Front Page for pre-2.1 WP.)

    Note: If you select a static Page as your frontpage (see below), this tag will be applied to your "posts page".
    [edit] The Front Page

    is_front_page()
    When it is the front of the site displayed, whether it is posts or a Page. Returns true when the main blog page is being displayed and the 'Settings > Reading ->Front page displays' is set to "Your latest posts", or when 'Settings > Reading ->Front page displays' is set to "A static page" and the "Front Page" value is the current Page being displayed. Note: this tag was added at Version 2.5.

  3. sp00f
    Member
    Posted 2 years ago #

    so basiclly how can i do this ?
    1 page, index.php, and if its front page some html code and if its not front page then all pages will get some other html.
    thats why im trying to do dunno what to use im abit confused

  4. Devin Price
    Member
    Posted 2 years ago #

    WordPress uses different templates for pages on your site. If you have a page.php template in your theme, that will be used to display your pages. If you have a single.php, that will be used to display your single posts. index.php or home.php would display your home page, or page.php if you have a certain page selected as the home page through the reading options.

    So, your conditional statement:

    <?php if (is_front_page()){ ?>
        <p>Home Page</p>
      <?php else { ?>
         <p>Not Home Page</p>
      <?php } ?>
    ?>

    Would only really be useful in the header.php or the footer.php file- or in page.php if you have a certain page selected in the reading options.

  5. sp00f
    Member
    Posted 2 years ago #

    but if i use home.php as my main page i have to make in options > reading first page view as "last posts" and then my first page will be a post and not a page so home.php is not really helping me, or there is a way to make home.php to be viewed by a normal page and not by pots only.
    thanks agian.

  6. vapvap
    Member
    Posted 2 years ago #

    Dear Moderator i think there where different kind to solve this problem.

    Try this in any php - header.php,index.php,page.php,single.php

    Example:

    <?php
    if (is_front_page())
    {?>
    
    <div>Flash Gordon</div>
    
    <?php } else { ?>
    
    <div>Wins the Battle!</div>
    <?php } ?>
  7. dlawton
    Member
    Posted 1 year ago #

    is_home fixed my problem, strange though I swear is_front_page worked previously, maybe something got changed

  8. kureikain
    Member
    Posted 1 year ago #

    It maybe you use new menu system and add Home as a menu item, correct?
    If so, is_front_page() will stop working

  9. kureikain
    Member
    Posted 1 year ago #

    Or use is_nav_menu_item() to find out "home" menu item

Topic Closed

This topic has been closed to new replies.

About this Topic