WordPress.org

Ready to get started?Download WordPress

Forums

Displaying the latest wordpress entries on my main page (11 posts)

  1. MattRamos
    Member
    Posted 9 years ago #

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

  2. rwatkins
    Member
    Posted 9 years ago #

    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

  3. MattRamos
    Member
    Posted 9 years ago #

    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.

  4. MattRamos
    Member
    Posted 9 years ago #

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

  5. Joshua Sigar
    Member
    Posted 9 years ago #

  6. MattRamos
    Member
    Posted 9 years ago #

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

    Any tips to get this to work? Thanks!

  7. Joshua Sigar
    Member
    Posted 9 years ago #

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

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

  8. MattRamos
    Member
    Posted 9 years ago #

    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?

  9. Joshua Sigar
    Member
    Posted 9 years ago #

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

  10. MattRamos
    Member
    Posted 9 years ago #

    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?

  11. MattRamos
    Member
    Posted 9 years ago #

    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 :P I have things working now.

    Thanks Rob and alphaoide

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.