Title: Adding custom PHP to WP pages
Last modified: August 19, 2016

---

# Adding custom PHP to WP pages

 *  [Andy](https://wordpress.org/support/users/andythreecoaching/)
 * (@andythreecoaching)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/)
 * 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](https://wordpress.org/support/users/t-p/)
 * (@t-p)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725491)
 * see if these threads help;
 * [http://wordpress.org/support/topic/adding-separate-php-filled-pages](http://wordpress.org/support/topic/adding-separate-php-filled-pages)
   
   [http://wordpress.org/support/topic/adding-php-code-to-wordpress-pages](http://wordpress.org/support/topic/adding-php-code-to-wordpress-pages)
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725493)
 * 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/](http://wordpress.pastebin.com/)
   and post the link to it here)
 *  Thread Starter [Andy](https://wordpress.org/support/users/andythreecoaching/)
 * (@andythreecoaching)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725496)
 * 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](https://wordpress.org/support/users/andythreecoaching/)
 * (@andythreecoaching)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725503)
 * 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?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725505)
 * 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](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/Function_Reference/is_page)
   [http://codex.wordpress.org/Conditional_Tags](http://codex.wordpress.org/Conditional_Tags)
   [http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page](http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page)
 *  Thread Starter [Andy](https://wordpress.org/support/users/andythreecoaching/)
 * (@andythreecoaching)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725521)
 * That’s great. I’ll look into that and get back.
 * Many thanks!
 *  Thread Starter [Andy](https://wordpress.org/support/users/andythreecoaching/)
 * (@andythreecoaching)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725627)
 * 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.
 *  [ke vinritt](https://wordpress.org/support/users/ke-vinritt/)
 * (@ke-vinritt)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725630)
 * 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](https://wordpress.org/support/users/andythreecoaching/)
 * (@andythreecoaching)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725796)
 * 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.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 4 participants
 * Last reply from: [Andy](https://wordpress.org/support/users/andythreecoaching/)
 * Last activity: [15 years, 6 months ago](https://wordpress.org/support/topic/adding-custom-php-to-wp-pages/#post-1725796)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
