WordPress.org

Ready to get started?Download WordPress

Forums

Adding custom PHP to WP pages (10 posts)

  1. andy@threecoaching
    Member
    Posted 3 years ago #

    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?

  2. Tara
    Member
    Posted 3 years ago #

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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)

  4. andy@threecoaching
    Member
    Posted 3 years ago #

    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... ?

  5. andy@threecoaching
    Member
    Posted 3 years ago #

    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?

  6. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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

  7. andy@threecoaching
    Member
    Posted 3 years ago #

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

    Many thanks!

  8. andy@threecoaching
    Member
    Posted 3 years ago #

    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.

  9. ke vinritt
    Member
    Posted 3 years ago #

    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

  10. andy@threecoaching
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.