If page is true then show div, else not

Discussion in 'PHP' started by unti62, Mar 4, 2013.

  1. #1
    Hi I've been working on this for a while now but I'm no expert in php. I'm trying to show a certain div only on the homepage.

    <div>

    <?php if(strpos($_SERVER['REQUEST_URI'],'/home/')===true)
    if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Header Widget Area Slider'))
    }else{

    <?php endif;?>

    <?php :?>
    </div>
     
    unti62, Mar 4, 2013 IP
  2. peter_anderson

    peter_anderson Notable Member

    Messages:
    3,382
    Likes Received:
    152
    Best Answers:
    0
    Trophy Points:
    240
    #2
    Do you use URL Rewrite to load your pages?

    Eg is example.com/home actually example.com/index.php?page=home?

    If so, you could change your code to:

    <?php
    if($_GET['page'] == 'home' OR empty($_GET['page'])){ // change the variable...
      // check for the function existing function
      if(function_exists('dynamic_sidebar'){
        // show the sidebar
        dynamic_sidebar('Header Widget Area Slider');
      }
    }
    ?>
    PHP:
     
    peter_anderson, Mar 4, 2013 IP
  3. unti62

    unti62 Peon

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #3
    Thanks for the quick reply!

    Not sure what URL rewrite is...? I've used wordpress to create the major part of the website but am now finetuning in the code. If wordpress comes with URL rewriting then yes, if not then I'm pretty sure the website does not use it as I didn't add any code as such.


    Your code gives a syntax error in my php editor on line 4. I'm a very beginner so fixing this myself is fairly impossible ;) If this expected?
     
    unti62, Mar 4, 2013 IP
  4. peter_anderson

    peter_anderson Notable Member

    Messages:
    3,382
    Likes Received:
    152
    Best Answers:
    0
    Trophy Points:
    240
    #4
    Sorry for the sytnax error, I typed it quickly and missed a bracket.


    Since you're using Wordpress, there is a function available within Wordpress to check for the homepage:

    Use
    is_home()
    PHP:
    if you have a static front page: http://codex.wordpress.org/Function_Reference/is_home
    Or
    is_front_page()
    PHP:
    if you use a front page (the page which shows posts in order of creation).

    You could also use
    is_page('PageName')
    PHP:
    to check the page name. http://codex.wordpress.org/Function_Reference/is_page
     
    peter_anderson, Mar 5, 2013 IP
  5. unti62

    unti62 Peon

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #5
    Thanks so much! Trick with wordpress codes worked!
     
    unti62, Mar 5, 2013 IP
    peter_anderson likes this.