WordPress.org

Support

Support » How-To and Troubleshooting » Displaying the latest wordpress entries on my main page

Displaying the latest wordpress entries on my main page

  • Hey, I’m putting a blog on my webdev site, and I decided on wordpress.

    You can take a look at http://www.mattramos.com/ to see what I’m trying to do. On the home page, I want to have 2 or 3 of the latest journal entries displayed in there. This is what I have now to grab the latest entry.

    $latestentries = (“SELECT * FROM wp_posts ORDER BY id DESC LIMIT 1”);
    $latestentriesresult = mysql_query($latestentries) or die(mysql_error());

    while($row = mysql_fetch_array($latestentriesresult)) {
    $entrytitle = $row[‘post_title’];
    $entrydate = $row[‘post_date’];
    $entrycontent = $row[‘post_content’];
    }

    mysql_query ($latestentries);

    When I put in LIMIT 2 or LIMIT 3 in place of the 1, whenever I echo $entrytitle, it displays all of the past ones. The same thing happens when I echo any of the other variables. So, basically.. I’m asking if you guys can give me a better way, or something to add to my code to get this to work.

    Other things I want to get up there eventually is a link to that entry, and I’m not sure how to do that either.

    Thank you everyone 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hey,

    You need to look up the mysql limit syntax:

    .... LIMIT base, number

    where base is the record you want to start at, and number is the number of records you want to pull out the database. So to take the first three records, you want (I think) LIMIT 1, 3.

    There are much nicer ways of pulling out ‘n’ records from the database than connecting to it directly yourself. If you include the WordPress files into your home page php (see here for info on how to do that), you can then take on all the functionality of WordPress. Then you can use the main post loop and just include a variable that restricts the number of posts to display.

    Something a bit like this:

    <?php $i = 0; if ( have_posts() ) : while ( have_posts() && $i < 3 ) : the_post(); ?>
    stuff to do with the posts
    <?php $i++; endwhile; endif; ?>

    Then you can use all the wordpress template tags instead :).

    Rob

    Thanks a lot! That’s exactly the kind of reply I was looking for. I’ll have to add the template tags to my homepage.

    Thanks again.

    Well, I did everything you said. I just copied the template tags from my theme’s index.php to it. I added <?php $i = 0; if ( have_posts() ) : while ( have_posts() && $i < 3 ) : the_post(); ?>

    and

    <?php $i++; endwhile; endif; ?>

    But when I go the page, I get this.

    Parse error: parse error, unexpected T_ENDWHILE in /home/mattra00/domains/mattramos.com/public_html/index.php on line 65

    that line turns out to be <?php $i++; endwhile; endif; ?>

    Thanks for the link, but Rob posted that above and I read through it.

    Any tips to get this to work? Thanks!

    <?php $my_query = new WP_Query('showposts=10'); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    // Do stuff.
    <?php endwhile; ?>

    I’m very new to this stuff. I’m not sure if I have to change anything in what you posted to get it to work.

    Right now, this is what I have.

    <?php $my_query = new WP_Query(‘showposts=10’); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <?php the_content(__(‘(more…)’)); ?>

    <?php endwhile; ?>

    I copied the the_content part from my theme, which works. Does it have to be done another way? Is there anything wrong as far as you can see?

    instead of the_content(), do
    $my_query->the_content()

    I have “<?php $my_query->the_content(); ?>” in place of the other line and it’s still not working. That was what I was supposed to do, right?

    Thanks for all of your help! I figured out that the problem was a setting in my ftp program which wasn’t letting me overwrite files 😛 I have things working now.

    Thanks Rob and alphaoide

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Displaying the latest wordpress entries on my main page’ is closed to new replies.