Display latest wordpress posts title

Discussion in 'PHP' started by asgsoft, Feb 1, 2009.

  1. #1
    Hey!

    Does anyone know how I can display the latest 5 wordpress post title into the main website?

    Currently wordpress is installed in a folder on the same server I want to display the posts links on.

    How can I do that?

    Thanks
     
    asgsoft, Feb 1, 2009 IP
  2. slpshtmike

    slpshtmike Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You will want to use a variation of "The Loop" that only prints out title.
    http://codex.wordpress.org/The_Loop

    To filter "The Loop" and only show your 5 latest posts, you will probably use the query_posts() method right before your loop.
    <?php query_posts($query_string . '&posts_per_page=5'); ?>
    PHP:
     
    slpshtmike, Feb 1, 2009 IP
  3. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Save the following code as latest.php in your wordpress folder
    <?php
    
    $numberOfPosts = 5;
    
    if (empty($wp)) {
        require_once('wp-config.php'); wp('feed=rss');
    }
    
    if ($posts) {
        $i = 1;
        foreach ($posts as $post) {
            start_wp();
    ?>
    
    <a href="<?php permalink_single_rss() ?>" /><?php the_title_rss() ?></a><br>
    
    <?php
            if (++$i > $numberOfPosts) {
                break;
            }
        }
    }
    ?>
    PHP:
    Then you can include it in your main index file thusly
    <?php
    
    include "wordpress/latest.php";
    
    ?>
    PHP:
     
    stoli, Feb 1, 2009 IP
    asgsoft likes this.
  4. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #4
    thanks a lot for that!

    it works a treat :D
     
    asgsoft, Feb 1, 2009 IP