Customizing the Home Page of Your CMS

I recently got a question from a reader following the "Creating a PHP CMS" series of tutorials asking about customizing the home page to be different from the rest of the site.

There are multiple ways of doing this. Here is one way:

if (!$_GET['id']) {
    // If this is the home page
}

Because the home page is loaded when there is no ID specified in the url query string, that means that if there is no ID specified, it must be the home page.

If you're looking for a slight cleaner method, perhaps if you want the home page to be the same whether you load the home page with its ID in the query string or not, you can create a function. To do this, add the following code into your functions.php file.

function isHome() {
    if ($_GET['id']) {
        $result = mysql_query('SELECT value FROM settings WHERE name="homePage"');
        $row = mysql_fetch_array($result);
 
        if ($_GET['id'] == $row['value']) {
            return true;
        } else {
            return false;
        }
    } else { // If no ID specified, it must be the home page.
        return true;
    }
}

When this function is called, it will check if an ID is specified. If none specified, it must be the home, and it returns true. If one is specified, it grabs the setting in the database containing the ID of the home page, and compares it with the ID requested.

To check if a page is the home page, just add the following code. Make sure to call it after the connect() function is called.

if (isHome()) {
    // ...
}

To add stuff that will only be shown on the home page, simply place it in between the curly braces. Alternatively, if you'd like to display stuff on all pages except the home page, use the following code:

if (isHome() == false) {
    // Stuff goes here.
}

Expanding on This

To expand on this even more, you can do things such as having certain things only display on certain pages of the site. I'll let you figure out how to do that, using what you learned in this post, or in the Creating a PHP CMS series.

This guide should work for everyone. If you have any problems, or if you have any questions, be sure to leave a comment, and I will do my best to help you out!

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!

Comments are closed.