Page 1 of 1

Limiting the number of posts on home page

Posted: Tue Jul 05, 2011 8:03 am
by nyo
Hi,

I am working on a blog and following the blog tutorial I was able to come to a good point. Now, I want to limit the number of posts on the home page (5 for example). How Can I do that?

Currently I have the following code in my index.php file:
<?php
	foreach($posts as $post) {
		?>
		<h1><a href="<?php echo $post['post_name']; ?>"><?php echo $post['post_title']; ?></a></h1>
		<p>Posted by <strong><?php echo $post['post_author']; ?></strong> on <?php echo date('j M, Y', strtotime($post['post_date'])); ?>
		   in <strong><a href="category.php?p=<?php echo $post['cat_name']; ?>"><?php echo $post['cat_title']; ?></a></strong>
		</p>
		<div>
		<?php
			echo $post['post_body'];
		?>
		</div>
		<?php
	}
	?>
I tried using something like for ($i = 1; $i <= 5; $i++) but it didn't work.

Re: Limiting the number of posts on home page

Posted: Tue Jul 05, 2011 10:13 am
by jacek
In your query you can add
LIMIT 0, 5
which will get the first 5 rows only.

It would probably make sense to add a parameter to the function to control this number as you may want to show more in other places.

Re: Limiting the number of posts on home page

Posted: Tue Jul 05, 2011 10:27 am
by nyo
jacek wrote:In your query you can add
LIMIT 0, 5
which will get the first 5 rows only.

It would probably make sense to add a parameter to the function to control this number as you may want to show more in other places.
Thanks Jacek, I should have thought of that. I will go through PHP and MySQL in detail soon but now I want to make my blog ready to some extent so that I can keep posting. Now I am on my way to check your pagination tutorial (Your tutorials are great.). I want to add something like "1 2 3 ..." links on the homepage and "next post" "previous post" links on the posts. And I still need to figure out how to add titles properly.