PHP If/Then Question

Discussion in 'PHP' started by FightersCosmos.com, Mar 19, 2009.

Thread Status:
Not open for further replies.
  1. #1
    Hey everyone. I want to use PHP in Wordpress to only display a certain element on just one page of the website. I was thinking of using if then to do it. So for instance I want it to be something like:

    If page that visitor is viewing equals x
    then show this
    else show this

    How do I do this in PHP?
     
    FightersCosmos.com, Mar 19, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    with pure php
    <?php
    $page = $_SERVER['PHP_SELF'];
    if($page == "/whichpage.php") {
    print "show this";
    }else{
    print "show this 2";
    }
    ?>
     
    crivion, Mar 19, 2009 IP
  3. FightersCosmos.com

    FightersCosmos.com Guest

    Messages:
    174
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Crivion, that slightly works.

    What should I put in the page variable if my page is not a PHP file. I'm in wordpress so my URL is like this:

    http://www.mysite.com/?page_id=130
     
    FightersCosmos.com, Mar 19, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    The "correct" way to do this in Wordpress would probably be to make a template file with a loop that only displays what you want on that page only, and then make a new page in Wordpress with that template.

    But you could exchange the
    $page = $_SERVER['PHP_SELF'];
    with
    $page = $_GET['page_id']; (if I'm assuming right, and the variable after the ? in the link is page_id
    and then exchange
    if($page == "/whichpage.php") {
    with
    if($page == "130") {

    assuming that the page number is 130, of course
     
    PoPSiCLe, Mar 19, 2009 IP
  5. FightersCosmos.com

    FightersCosmos.com Guest

    Messages:
    174
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Popsicle, that worked. PHP is awesome, can anyone recommend any learning resources that will help me get to know this language quickly?
     
    FightersCosmos.com, Mar 19, 2009 IP
  6. Devant

    Devant Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am using this for reference mostly, but is an amazing collection for tutorials, not only for PHP, but for whatever you are going to use along with PHP:

    http://www.w3schools.com/
     
    Devant, Mar 19, 2009 IP
Thread Status:
Not open for further replies.