Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Is there a better way to manage website menus ... without needing to edit every page individually?

is there a better way to manage menu content that is identical across all pages (for websites with 25 or more pages)? i.e. adding a new <li> to the menu so that it updates on all pages from one control document?

1 Answer

Yes. Look at the building a basic website in the PHP course. You learn how to make files that contain blocks of code and reference them in other files among a ton of other things. The short answer is to change your file type to php, so it becomes index.php instead of index.html. Then put your nav in another file called nav.php (or whatever else you want). Or you can put your head and header in there as well if you want. Then import the file like this:

<?php /*opening PHP tag*/
include("path/to/nav.php");
?> //closing PHP tag

You can put in whatever you want before or after the php opening and closing tags. Here are some examples from my main learning project right now:

//this includes PHP constants, sets some PHP variables, and includes the entire head and header. 
//The head, header, and nav are separate files that are built into the header.php prior to inclusion into index file
<?php
require_once("../inc/config.php");
$pageTitle = "Pack 198 Dens";
$section = "dens";
$description = "Pack 198 Cub Scout Dens";
include_once(ROOT_PATH . "inc/header.php");
?>

//I also import the footer
<?php include_once(ROOT_PATH . "inc/footer.php"); ?>

//and this is the header file
<?php require_once("config.php"); ?>

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9" lang=""> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang=""> <!--<![endif]-->

<?php include_once(ROOT_PATH . "inc/head.php"); ?>

<body>
        <!--[if lt IE 8]>
            <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->
    <div class="main-wrapper">
        <div class="main-header">
            <?php include(ROOT_PATH . "inc/nav.php"); ?>
        </div>

As you can see, there is a ton you can do to DRY your code through PHP. I suggest the PHP course track as a whole. I am finishing a course on a simple framework and using some really cool tools that will make life so much easier. I am going to refactor this entire site with what I am learning now. You can check out the site at http://vetsrights.com if you want. Just be warned the JavaScript aon the home page is not displaying properly and I have not figured it out yet.