Support » Plugins » 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)
  • 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 😀

    just tested it to make sure it works and it does 🙂 on a default WP install at least

    Thread Starter hjo3

    (@hjo3)

    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 🙂

    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

    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.