Title: PHP code for latest post title?
Last modified: August 18, 2016

---

# PHP code for latest post title?

 *  [hjo3](https://wordpress.org/support/users/hjo3/)
 * (@hjo3)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-latest-post-title/)
 * Hi.
 * Is there some simple PHP code that can display the title of the latest post? (
   E.g. something like `<?php latest_post_title(); ?>`) I use Exec-PHP and I’m trying
   to create a page that mentions the title of the latest post without me having
   to edit it every time.
 * Disclaimer: I’m shaky on PHP and the inner-workings of WordPress. Thanks

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

 *  [Phunky](https://wordpress.org/support/users/phunky/)
 * (@phunky)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-latest-post-title/#post-405316)
 * if all you want is the title of the latest post and i assume it linking to it
   aswell use the following
 * `<?php
    foreach($wpdb->get_results("SELECT ID,post_title FROM 
   wp_posts WHERE
   post_status = 'publish' ORDER BY post_date DESC LIMIT 0,1") as $p){
    echo "<h1
   ><a href="".get_bloginfo('siteurl')."/?p=".$p->ID."">".$p->post_title."</a></
   h1>"; } ?>
 * This “Should” just pull out the last published post and display it in a H1 with
   a link to the post.
 * Its quick and dirty and ive not tested it so dont shout if it dont work 😀
 *  [Phunky](https://wordpress.org/support/users/phunky/)
 * (@phunky)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-latest-post-title/#post-405318)
 * just tested it to make sure it works and it does 🙂 on a default WP install at
   least
 *  Thread Starter [hjo3](https://wordpress.org/support/users/hjo3/)
 * (@hjo3)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-latest-post-title/#post-405335)
 * Hmm, I get an error with it:
 * Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘,’
   or ‘;’ in /home/.kathlee/hjo3/hjo3.net/wordpress/wp-content/plugins/exec-php.
   php(45) : eval()’d code on line 3
 * Any idea why? Again, my php skills are quite lacking. Thanks 🙂
 *  [Phunky](https://wordpress.org/support/users/phunky/)
 * (@phunky)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-latest-post-title/#post-405368)
 * Erm, i guess this is cos your using it thru exec-php – i dont use that so have
   no way of knowing whats wrong with it.
 * If you just create a page template and use that inside the pages template it 
   will work
 *  [basketball](https://wordpress.org/support/users/basketball/)
 * (@basketball)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-latest-post-title/#post-405667)
 * Erm, change:
 * `<?php
    foreach($wpdb->get_results("SELECT ID,post_title FROM wp_posts WHERE 
   post_status = 'publish' ORDER BY post_date DESC LIMIT 0,1") as $p){ echo "<h1
   ><a href="".get_bloginfo('siteurl')."/?p=".$p->ID."">".$p->post_title."</a></
   h1>"; } ?>
 * to
 * `<?php
    foreach($wpdb->get_results("SELECT ID,post_title FROM wp_posts WHERE 
   post_status = 'publish' ORDER BY post_date DESC LIMIT 0,1") as $p){ echo "<h1
   ><a href=".get_bloginfo('siteurl')."/?p=".$p->ID.">".$p->post_title."</a></h1
   >"; } ?>
 * There were two extra quotation marks in the a href part. Anyways, thanks for 
   posting the code phunky, I was stumped on this for a while. Using it on my site
   right now 🙂

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

The topic ‘PHP code for latest post title?’ is closed to new replies.

## Tags

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

 * 5 replies
 * 3 participants
 * Last reply from: [basketball](https://wordpress.org/support/users/basketball/)
 * Last activity: [19 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-latest-post-title/#post-405667)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
