Title: Echo Root Title
Last modified: August 20, 2016

---

# Echo Root Title

 *  Resolved [ajcke](https://wordpress.org/support/users/ajcke/)
 * (@ajcke)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/)
 * The following code echos the root page id. How can I get it to echo the root 
   page title instead of the id number? I’d also like to make it a link.
 *     ```
       <?php $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors ); ?>
       <h2><?php echo $root_page_id; ?></h1>
       ```
   

Viewing 14 replies - 1 through 14 (of 14 total)

 *  [asarosenberg](https://wordpress.org/support/users/dotnordic/)
 * (@dotnordic)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549630)
 * `<?php echo get_the_title($root_page_id);?>`
 *  Thread Starter [ajcke](https://wordpress.org/support/users/ajcke/)
 * (@ajcke)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549667)
 * That does the trick. On the home page it this code displays the latest post title.
   I would probably need some type of if statement to detect the homepage and display
   the site title. What code could I use for that?
 *  Thread Starter [ajcke](https://wordpress.org/support/users/ajcke/)
 * (@ajcke)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549681)
 * And one more thing. When a post page is displayed the code displays the post 
   title. How could this code display the site title instead of the post title?
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549689)
 *     ```
       <?php $page_title = ( empty( $post->ancestors ) ) ? get_bloginfo('name')  : get_the_title(end( $post->ancestors )); ?>
       <h2><?php echo $page_title ; ?></h1>
       ```
   
 *  Thread Starter [ajcke](https://wordpress.org/support/users/ajcke/)
 * (@ajcke)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549701)
 * I tried the following code to resolve the homepage issue. It gave my site the
   white screen of death and if I remove the code I can’t get my site back.
 *     ```
       <?php
       if ( is_home() ) {
           <h1><?php bloginfo('name'); ?></h1>
       } else {
           <?php $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors ); ?>
           <h1><?php echo get_the_title($root_page_id);?></h1>
       }
       ?>
       ```
   
 * _[Please post code & markup between backticks or use the code button. Your posted
   code may now have been permanently damaged by the forum’s parser.]_
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549702)
 *     ```
       <?php
       if ( is_home() ) {
           ?><h1><?php bloginfo('name'); ?></h1><?php
       } else {
           $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors ); ?>
           <h1><?php echo get_the_title($root_page_id);?></h1>
       }
       ?>
       ```
   
 *  Thread Starter [ajcke](https://wordpress.org/support/users/ajcke/)
 * (@ajcke)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549718)
 * I’m banging my head over this one. I get a white screen with the following code.
   I don’t see any issues with it.
 *     ```
       <?php
       if ( is_home() ) {
       // This is a homepage
       <h1>Homepage</h1>
       } else {
       // This is not a homepage
       <h1>Not Homepage</h1>
       }
       ?>
       ```
   
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549719)
 * You aren’t closing off your PHP or stringifying the HTML.
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549720)
 * Why aren’t you using the code I suggested?
    Why do you keep changing on your 
   own?
 *  Thread Starter [ajcke](https://wordpress.org/support/users/ajcke/)
 * (@ajcke)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549721)
 * I tried, but my site displays a white screen with the if statement code. Can 
   you post an example of what it should look like? Is `?>` not properly closing
   the php?
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549722)
 * I already did twice, see above.
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549723)
 * Go with Rajesh’s example.
 *  Thread Starter [ajcke](https://wordpress.org/support/users/ajcke/)
 * (@ajcke)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549729)
 * Rajesh your first snippet of code works perfectly. I’m not sure why it wasn’t
   displaying correctly…user error!
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549730)
 * Awesome!

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Echo Root Title’ is closed to new replies.

## Tags

 * [code](https://wordpress.org/support/topic-tag/code/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [root page](https://wordpress.org/support/topic-tag/root-page/)
 * [title page](https://wordpress.org/support/topic-tag/title-page/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 4 participants
 * Last reply from: [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/echo-root-title/#post-3549730)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
