WordPress latest post on home page

I have created my own theme and include the code for the main div of the home page below. What I want to do is to include just the latest post and have the pagination for earlier posts. I guess I’m missing something rather obvious as the pagination code. I realise the pagination code is redundant as it’s outside the while loop. Thanks!

<div id="main">

  <?php if ( have_posts() ) : ?>
  <?php while ( have_posts() ) : the_post(); ?>
  <div <?php post_class(); ?>>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p class="published">Posted on
    <?php the_time('j F Y'); ?>
    by <?php the_author(); ?></p>
    <?php the_content(); ?>
  </div>
  <?php endwhile; ?>

  <div class="pagination">
    <?php next_posts_link('Older'); ?>
    <?php previous_posts_link('Newer'); ?>
  </div>

  <?php else : ?>
    <h2>Nothing found</h2>
    <p>Sorry, but there is nothing here to see.</p>

  <?php endif; ?>

</div>

I’m also having trouble inserting code in this new forum!

No worries, I fixed it. You may want to read the following post:

Have you tried using WP_query() to just request the most recent post for display? There is a list of many different parameters that you can use with it to customize which posts you want to pull from the database here: http://codex.wordpress.org/Class_Reference/WP_Query as well as examples. If you are still stuck after you check out this resource, let us know.

Thanks for fixing my post cpradio!

Ah, thanks WebMachne. I will look at that. I have a book on WordPress but I think it’s a bit out of date now…

I have changed my code to:

  <?php
  $the_query = new WP_Query('posts_per_page=3'); ?>

  <?php if ( $the_query->have_posts() ) : ?>

  <!-- pagination here -->

  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <p class="published">Posted on
  <?php the_time('j F Y'); ?>
  by <?php the_author(); ?></p>
  <?php the_content(); ?>
  <?php endwhile; ?>

  <!-- pagination here -->
  <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
  <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

  <?php wp_reset_postdata(); ?>

  <?php else : ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>

and I get 3 posts on the home page, but the pagination doesn’t seem to be working…
UPDATE
Okay, so I changed the next_posts_link to:

next_posts_link('Older posts', $the_query->max_num_pages)

and I get the older posts link but if I click on it I get a page not found page. The blog is at
http://www.researcheducatedevelop.com/blog/

Add ‘paged = $paged’ as in the code below to add pagination:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query('posts_per_page=3&paged=' . $paged');
?>

Great - many thanks! G :slight_smile:

I’ve just noticed that although it seems to work I’m still getting page not found - only since I don’t have a 404.php file it’s not obvious.

I can’t seem to get your link to work

http://www.researcheducatedevelop.com/blog/ - seems okay now. Must have been a temporary glitch. I changed your $my_query to $the_query since that is what I use elsewhere. I assume that was the right thing to do.

I realise I probably didn’t phrase my last post very well. The link to the blog is working again now but I still have a problem with the pagination. I have changed it so there are 2 posts to a page. As it happens there are currently only 4 posts on the blog. I get the “older posts” link as well as the newest 2 posts on the first page but here’s the rub: if I click on the older posts link I actually get the 2 older posts BUT looking at the page title it says Page not found.

The pagination seems to work for me. Could we see the code you have in your header.php to see if there is an issue with the element?

Ah - do you not get Page not found « Life in the Research Lane as the page title for the older posts? My header.php is:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<title><?php wp_title('&laquo;', true, 'right'); ?> <?php bloginfo('name'); ?></title>
<link href="<?php bloginfo('stylesheet_url'); ?>" rel="stylesheet" type="text/css" />
<meta name="description" content="Life in the Research Lane : the Research Educate Develop blog" />
<meta name="viewport" content="width=device-width" />
<?php wp_head(); ?>
</head>
<body>
<div id="wrapper">
  <div id="header">
    <div id="hdr-left"><a href="/index.php" title="Home page"><img src="<?php bloginfo('template_url'); ?>/images/logo-blog.png" alt="logo" width="287" height="114" /></a></div>
    <div id="hdr-right"><a href="https://www.facebook.com/ResearchEducateDevelop" title="Follow us on Facebook" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/facebook_32.png" alt="facebook" width="32" height="32" /></a> <a href="http://www.linkedin.com/company/research-educate-develop" title="Follow us on LinkedIn" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/linkedin_32.png" alt="linkedin" width="32" height="32" /></a> <a href="https://twitter.com/ResearchEduDev" title="Follow us on Twitter" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/twitter_32.png" alt="twitter" width="32" height="32" /></a> <a href="https://www.youtube.com/channel/UCsFREuMbvnW1zMwAbO73_0Q" title="Visit our YouTube channel" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/youtube_32.png" alt="Youtube" width="32" height="32" /></a> <a href="<?php echo get_option('home'); ?>" title="Visit our blog"><img src="<?php bloginfo('template_url'); ?>/images/blog-icon_32.png" alt="Blog" width="32" height="32" /></a> <a href="../rss.xml" title="RSS feed"><img src="<?php bloginfo('template_url'); ?>/images/rss_32.png" alt="RSS" width="32" height="32" /></a></div>
  </div>
  <!-- end header -->

Many thanks for you help!

What do you want as your page title? On the first page, it looks like you have your site description.

As far as I know, wp_title is used for single posts, or pages. Have you tried something like this:

<title>
    <?php
            if ( function_exists( 'is_tag' ) && is_tag() ) {
                single_tag_title( "Tag Archive for &quot;" );
                echo '&quot; - ';
            } elseif ( is_archive() ) {
                wp_title( '' );
                echo ' Archive - ';
            } elseif ( is_search() ) {
                echo 'Search for &quot;' . exc_html( $s ) . '&quot; - ';
            } elseif ( is_404() ) {
                echo 'Not Found - '; 
            }
            
            if ( is_home() ) {
                bloginfo( 'name' );
                echo ' | ';
                bloginfo( 'description' );
            } else {
                wp_title('&laquo;', true, 'right' );
                bloginfo( 'name' );
            }            
            
            if ( $paged>1 ) {
                echo ' - page ' . $paged;
            }
    ?>
</title>

Geez - I didn’t realise the title needed to be so complicated! Thanks. I’ll see what that does for me. The title seemed fine till I tried the pagination. Thanks WebMachine

It doesn’t have to be so complicated. I just like to use this one in all the themes I code, because it covers all the bases so I don’t have to customize every single one.

I think your title works fine on the first page because it is the home page of your blog, but when you go to the older posts, it doesn’t know which post title to use for wp_title, so it resorts to “page not found”. wp_title() usually picks up the title of a post if it is a single post on the page.

I would be very happy if someone could correct me if I’m wrong, or explain to you more clearly. :smile:

Perfectly clear, squire. Thanks again! G :slight_smile:

use this code for latst post . only put your div tag according design.

<div main>

<?php if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

            <div class="post" id="post-<?php the_ID(); ?>">
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                <div class="entry">
                    <?php the_content('Read the rest of this entry &raquo;'); ?>
                </div>

                <p class="postmetadata"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_ID(); ?></a> | posted at <b><?php the_time('F jS, Y') ?></b> in <?php the_category(', ') ?> <?php the_tags('| Tags: ', ', ', ''); ?> <?php edit_post_link('Edit', ' | ', ''); ?></p>
            </div>

        <?php endwhile; ?>

        <div class="navigation">
            <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
            <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
        </div>

    <?php else : ?>

        <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.</p>
        <?php include (TEMPLATEPATH . "/searchform.php"); ?>

    <?php endif; ?>
</div>

Regards :<snip/>

Thanks Tech Buddy!