• Hi, I’m a new WP user. I’m a webmaster for a small pubication and some of my editors are using wordpress on the site. So far, so good.

    Now we’d like to pull the 5 most recent blog posts out of WP and display them as links in a section of the front page. The front page uses php and a bunch of custom built content management libraries to generate other lists of stuff. What php files do i have to include() and what functions can i call to get links to the 5 most recent articles?

    Thanks in advance for any help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • One ‘detached’ way is to use an RSS aggregator (like my CG-FeedRead script) to grab the WP RSS feed every maybe hour or two, and use that to display the latest 5 items in the feed. I know a lot of folks use that approach to separate the blog code from the main code.

    Otherwise, my guess is you’d have to include wp-blog-header, with a custom query set up so that “The Loop” gives you back the last five posts, and you’d just go through the ‘normal’ loop process but only output the titles/permalinks and no other content. Seems wasteful, especially if the blog isn’t updating that often.

    -d

    Thread Starter jacobrobbins

    (@jacobrobbins)

    aha… I think an easier way to do it is to grab the info directly from the mysql database table ‘wp_posts’. The field ‘guid’ gives a link to the post (not in pretty format thought). Thank you wordpress for making your database layout simple and understandable.

    Just to play devil’s advocate:

    – you shouldn’t directly pull from the SQL db, as the format could change in the future.
    – if your SQL doesn’t have caching enabled, you’re going to be hitting it every pageload unless you cache yourself.
    – the logic of which posts you should be showing is more complex than the last n entries in the table. they could be private, drafts, etc.
    – thus, you want to either use normal WP calls, or detach yourself and use something like an aggregator.
    – the advantage of an aggregator is that it is completely separate from the call structure of the blog (the blog could move, the SQL db could relocate, you could move to a different blog system, etc.), and it’ll still work. It also caches at its level, so the only time you’ll get a db lookup is at most once-per cache refresh.
    – the disadvantage of an aggregator is that you don’t necessarily get ‘instant’ updates forward. you only get them at cache-refresh intervals.

    Good luck with it!

    -d

    I tend to disagree with Mr. Chait;

    While it’s true that using a feed is inherently more flexible then hardcoding a db query, it’s also a whole lot more work for the average joe – who doesn’t specialise in XML.

    Also, running a simple query per page-load even without caching isn’t going to cause any problems for the vast majority of WP users.

    RE: showing drafts and so on accidentally, it’s a simple matter or adding into your SQL query “WHERE post_status=’publish'” to make sure you don’t get any but the posts you want.

    After about a minutes work I ended up with this query;

    <h4>Recent Articles</h4>
    <ul>
    <?
    $db = mysql_connect("localhost","db-user","db-password") or die("Problem connecting");
    mysql_select_db("db_name") or die("Problem selecting database");

    $recentblog = mysql_query("SELECT * FROM wp_posts WHERE post_status='publish' ORDER BY post_date DESC LIMIT 5");

    while($row = mysql_fetch_array($recentblog)){
    // Build your formatted results here.
    echo "<li><a href="".$row['guid']."">".$row['post_title']."</a></li>";
    }
    ?>
    </ul>

    I’d say that using something along those lines is going to be much more practical for most WP users.

    As for Jacob’s ugly GUI problem, I’ve messed around with my wordpress quite alot so I don’t know if I’d be getting a-typical results, but it seems all my posts bar the first few had their correct ‘pretty’ GUI addresses.

    Otherwise you’re going to have to run a query that’s a bit more complex in order to grab the category name for each post, appending the stub [post_name] to the end of it.

    ie. echo "<a href="https://yourblogaddress.com/$category/".$row['post_name']."">".$row['post_title']."</a>";

    In that case – and again, I’m not sure how prevalent the wrong-GUI thing will be – I guess it may be more practical for one to use a feed-based list.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘embedding post titles in non-wp pages’ is closed to new replies.