WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Include WP snippets on static HTML home page (5 posts)

  1. Yulorian
    Member
    Posted 7 years ago #

    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 --

    <?php
    define('WP_USE_THEMES', false);
    require('./wordpress/wp-blog-header.php');
    ?>

    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:

    <?php
    $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.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

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

    Here's a somewhat simpler way:
    <?php
    $my_query = new WP_Query('showposts=3');
    while ($my_query->have_posts()) {
    $my_query->the_post();
    the_title();
    the_content();
    whatever_you_like();
    }
    ?>

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

    Explanation:
    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.

    Simple.

  3. Yulorian
    Member
    Posted 7 years ago #

    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.

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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>

  5. Yulorian
    Member
    Posted 7 years ago #

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

    Thank you, Otto.

Topic Closed

This topic has been closed to new replies.

About this Topic