Support » Fixing WordPress » Include WP snippets on static HTML home page

  • Resolved Yulorian


    Okay, I’m working on a site that is using WordPress as the blog engine in ./wordpress/ folder. I’ve got my own CSS design for the main pages – the blog is separated somewhat from the rest of the site.

    I want to include snippets from the posts on the main static page (index.php). I have already figured out how to include the name of the post and a link, but I’d like to get the time and the first sentence or two as well.

    With some fiddling and some help from the Codex, I managed to get the first part working without messing with my design. The problem is I don’t know the PHP code/variables to call in order to get those things to appear.

    This is the code I’m using now:

    At the top of the document —

    define('WP_USE_THEMES', false);

    That allowed me to call the WP stuff without using any of the formatting style.

    Now, in the place where I’m calling the blog posts:

    $how_many=3; //How many posts do you want to show ?>
    <ul class="whats-new">
    $news=$wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts
    WHERE post_status = "publish" ORDER BY ID DESC LIMIT ".$how_many);
    foreach($news as $np){
    printf ("<li><a>%s</a></li>", $np->ID,$np->post_title);

    This code is giving me the post titles, but I can’t figure out what the variables are that I need and where to put them.

    Can anybody help me figure this out? Many thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    That’s a rather interesting, but difficult, way to do it, I suppose.

    Here’s a somewhat simpler way:
    $my_query = new WP_Query('showposts=3');
    while ($my_query->have_posts()) {

    This is a basic “Loop” and it makes all the Template Tags available to you for use inside that while loop.

    The WP_Query takes all the work out of querying the database for posts. It does the SQL so you don’t have to.
    The have_posts() returns true as long as there’s still results from your query.
    The the_post() call gets the next post and does some internal setup so that the Template Tags actually work.
    Then you call the Template Tags to display the post itself.




    Thanks very much, Otto.

    I just have one other question (because I’m still new at this). Those things come up as plain text. How would I go about turning the title into a link to the post, and formatting the text so that it doesn’t all run together? Right now it appears like this:

    Hello world!December 6, 20069:14AM

    Do I need to break up the PHP code and put in some standard HTML formatting tags, or is there more to it than that?

    Once again, thanks so much for your help. You certainly made this easier.

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Do I need to break up the PHP code and put in some standard HTML formatting tags, or is there more to it than that?

    No, that’s basically correct. Formatting the stuff is up to you.

    However, for the specific question of linking the title, you can use the permalink function. Handy.

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>



    Excellent! I think I’ve got the hang of it now!

    Thank you, Otto.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Include WP snippets on static HTML home page’ is closed to new replies.