Writing a Blog Loop

posted .

This post describes how I made my blog without a CMS. I warn you: I'm not an experienced programmer or a security expert. However, if you find this useful, feel free to use/modify the code.

When I was in the process of making my personal site, I knew that if I was going to run a blog, I would probably need a Content Management System (CMS)… or would I? I looked at the obvious choices like Wordpress and Joomla and then I looked to other systems like Textpattern and Concrete5. I searched tirelessly but there were a few turn-offs in all of them. For one, I didn't like the idea of the CMS controlling my website structure; that was something I wanted to create myself. Secondly, I didn't like the idea of using a database to store my posts considering I'm still in the process of learning how to use one, I don't have a need for it (with such a small blog) and it is slower than using a "flat-file" CMS. Lastly, I didn't like how markup was generated: often with long and unnecessary class names, too many or few tabs (and newlines) and a style that just wasn't me. This isn't to mention that I'd have to split up my existing static template into a bunch of smaller template files.

So, what did I do? I made my own “CMS.” Okay, so it's not really a CMS at all. It's simply a loop that goes through blog posts and puts them on a page. I feel that I don't need a feature-packed CMS for my website right now and I don't need to fool with a sluggish database. I could resort to a flat-file CMS like Pico or Grav but that wouldn't be as fun (or risqué).

This loop of mine simply displays summaries of my posts in reverse chronological order with a link to the full article. The idea is simple and to the point. Since I decided to make it flat-file, I just have to know how to manage the file system. Easy peasy. I started out with a folder containing folders called “Posts,” “Media” and “Summaries.”

The Posts folder contains the posts, the Media folder contains images and other assets and the Summaries folder contains separate snippets of articles which will be added to our blog's “index” page with include statements. The summaries will show up on our index page and will redirect to the individual post pages (conveniently placed in the Posts folder). For ease of sorting, I named each file in the Summary folder “post-1.php,” “post-2.php” and so on. The code in our index page, below will simply loop through these summary files and then display buttons to navigate the pages of posts.

$numberOfPosts = 2; //change to match number of blog entries
if(isset($_GET["page"])){
	$pageNumber = $_GET["page"];
} else {
	$pageNumber = 1;
} //get current page number from query string if it exists, otherwise set pageNumber to 1
$olderButton = $pageNumber + 1;
$newerButton = $pageNumber - 1;
if($pageNumber <= 0){
	$pageNumber = 1;
}
$postsPerPage = 5; //can be changed without breaking loop
$startingPoint = $numberOfPosts - (($pageNumber - 1) * $postsPerPage);
$endingPoint = $startingPoint - ($postsPerPage - 1);
if($endingPoint <= 0){
	$endingPoint = 1; //in case we don't have many posts
}
$nameOfFile = "";
while($startingPoint >= $endingPoint){ //the actual blog loop
	$nameOfFile = "blog/summary/post-" . $startingPoint . ".php";
	if(file_exists($nameOfFile)){
		include $nameOfFile;
	}
	$startingPoint -= 1;
}
//now we need to show the blog navigation to the reader
if($endingPoint != 1){
	echo "<div class=\"blog-button\"><a href=\"/blog.php?page=" . $olderButton . "\"><p>Older</p></a></div>" . "\n";
}
if($pageNumber > 1){
	echo "<div class=\"blog-button\"><a href=\"/blog.php?page=" . $newerButton . "\"><p>Newer</p></a></div>" . "\n";
}

That's it! That's the blog loop. It's simple, doesn't fool with a database, requires little PHP knowledge and doesn't require a CMS. Is it of any use outside of this small blog? Maybe. Probably not. A larger blog would require a database, a basic management interface and more reusable code. Regardless of practical use, this shows that setting up a basic loop for displaying blog posts isn't that complicated at all and doesn't necesarily require a big, fancy CMS.