EyeStorm Studio

Standards and Browser Aware Web Development

Standards and Browser Aware Web Development

Eyestorm is the online studio of David Wranovics, freelance web developer, owner and operator of the forum skin retail stores, ForumMonkeys and vBmode, founder of the original, now umbrella parent company, NetAtom COM. David supplements his larger projects with development partners he has met over the decade he has spent in web development.


Wordpress Loop on Non-Wordpress Pages

I was looking for an easy code snippet to put the latest blog posts on the EyeStorm index page using the Wordpress loop. I remember it took me quite a bit of searching, that I had to keep trying different keyword combinations until I finally found something to help me. Today, I wanted to change the sort order of the posts and no matter if I changed it from ASC to DESC, nothing happened. I realized the code I had pilfered had some syntax errors. Having some PHP/MySQL knowledge, I fixed them and spent another ten minutes looking for that post again so I could add a comment with the syntax fix to no avail. So, I decided I would post this in case other people are looking for the same thing and having problems finding it.

Now that I’ve bored you with the short story, here’s the code.

<?php
        $how_many=3; //How many posts do you want to show
        require_once("blog/wp-config.php"); // Change this for your path to wp-config.php file ?>

        <ol id="whats-new">
        <?php
        // if post state is published and the post type is a post (and not a page), display it. You can remove the  AND 'post_type' = \"post\"  line to grab both posts and pages. Sort ASC or DESC (Descending).
        $news=$wpdb->get_results("SELECT `ID`,`post_title` FROM $wpdb->posts
        WHERE `post_status`= \"publish\" AND `post_type` = \"post\" ORDER BY POST_DATE DESC LIMIT ".$how_many);
        foreach($news as $np){
        printf ("<li style='padding-bottom:7px;'><a href=\"blog/index.php?p=%s\">%s</a></li>", $np->ID,$np->post_title);
        }?>
        </ol>

5 Responses to “Wordpress Loop on Non-Wordpress Pages”

  1. David Says:

    Revised the code today to add some more select options, fixed a bug in the code.

  2. Steven Says:

    Hi David,

    I am currently trying to design a site with WP as a content management system. The landing page is currently HTML/CSS; I would like it when a page is created a link is added to the home page. I am using [] to create a menu for the pages, on the fly.

    Can I adapt your script to display the pages on my home-page, rather manually creating the links?

    Any help would be greatly appreciated.

    Regards,
    Steven.

  3. David Says:

    The “Just Blogged” links on the EyeStorm homepage are using the same PHP code as listed here. With every new blog post (granted there haven’t been many, too busy) those linsk are dynamically updated. No need to add manual links every again :)

  4. Vinnie Ryan Says:

    Is it possible to remove the href and fetch only the post content? It would be helpful to post content from membership pages to tempt the visitors to sign up for membership

  5. Vinnie Ryan Says:

    Here’s the code that fetches the content

    get_results(”SELECT `ID`,`post_title`,`post_content` FROM $wpdb->posts
    WHERE `post_status`= \”publish\” AND `post_type` = \”post\” ORDER BY POST_DATE DESC LIMIT “.$how_many);
    foreach($news as $np){
    printf (”%s“, $np->ID,$np->post_content);
    }?>

Leave a Reply