• Andy

    (@andythreecoaching)


    Just want to do a basic php if else on a page but when i do so and publish it, it just flops.

    Something i’m doing wrong?

    The file i’m trying to work on is header.php which has a few <?php instances in it, so no idea why it doesn’t like my code?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator t-p

    (@t-p)

    can you define ‘flop’?

    any error messages or warnings?

    what is the code you added in the context of the existing header.php?

    (for longer code, use a http://wordpress.pastebin.com/ and post the link to it here)

    Thread Starter Andy

    (@andythreecoaching)

    Actually, it may be my php. I’m rusty with php.

    Here it is:

    <?php
    if (page-id=="20") {
    
    <div id="homelinks">
    
    Bla bla, div content goes here...
    
    </div>
    
    } else {
        echo "";
    }
    ?>

    I’m trying to establish ‘if’ you’re on the home page then display this div, otherwise do nothing. I noticed that pages have an id and that the home page is 20. So this is my guess… ?

    Thread Starter Andy

    (@andythreecoaching)

    Actually i think it’s my bad.

    I thought that the pages had a ‘page-id’ variable. But i just did this and it came out negative:

    <?php
    
    if (defined('page-id')) {
        echo "x";
    }
    
    else {
    
    echo "y";
    }
    
    ?>

    So… is there a way to check if we’re on a certain page?

    unfortunately, programming does not work with guessing – if unsure, read the documentation.

    this might work:

    <?php
    if ($post->ID==20) { ?>
    
    <div id="homelinks">
    
    Bla bla, div content goes here...
    
    </div>
    
    <?php } else {
        echo "";
    }
    ?>

    always make sure to close the php tags before starting html tags, and vice versa.

    page-id is neither a valid php variable, nor holds it the page ID.
    the page ID is normally $post->ID

    php variables do not have hyphens –
    for instsnce: http://www.tizag.com/phpT/variable.php

    edit:
    just saw your question:

    So… is there a way to check if we’re on a certain page?

    there is: is_page(20)
    http://codex.wordpress.org/Function_Reference/is_page
    http://codex.wordpress.org/Conditional_Tags
    http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page

    Thread Starter Andy

    (@andythreecoaching)

    That’s great. I’ll look into that and get back.

    Many thanks!

    Thread Starter Andy

    (@andythreecoaching)

    Ok, i got the is_page to work. Those links were most useful.

    But I just need a further tip in how to do something…

    I want it to say:

    <?php 
    
    if (is_page('About 3C')) {
    
    <div>Display this particular div</div>
    
    }
    
    else {
    
    <div>Display that particular div</div>
    }
    
    ?>

    At the moment, it throws out a php error. I used to be able to do this in coldfusion, anything after the if statement would just display/continue the html code, so we’d see the resultant div tags.

    refer to alchymyth’s earlier post in reference to opening and closing php tags:

    <?php

    if (is_page(‘About 3C’)) {?>

    <div>Display this particular div</div>

    <?php }

    else { ?>

    <div>Display that particular div</div>
    <?php }?>

    try that

    Thread Starter Andy

    (@andythreecoaching)

    Took me a while to get around to trying this but thanks, it works a treat 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Adding custom PHP to WP pages’ is closed to new replies.