Tip of The Day: How Do You Include HTML in PHP? 1


HTML TipYou  may have already found that you need to include a paragraph or 2  of additional information on a dynamic section of your website.

This is especially true if you are using a Content Management System (CMS) such as WordPress or Drupal or Joomla etc. So how do you include HTML in your PHP coded page?

Or, if you are creating your own website from scratch you might be looking to include a menu in your template so that the same menu appears on every page in the exact same position.

How to Include Files in a PHP Page

This is a common question from people who have set up a WordPress site or blog.

You have installed your theme and now you find that there is other information that you want to include on one of the dynamic pages.

For example, here is a WordPress theme that is well set up as a responsive design but could be a little confusing for people with little programming knowledge.In other words, you are provided with an area where you can enter information on the home page but it can be a little cumbersome if you have a lot of information to enter.

IF

So, let’s say that you want to have 1/2 page of information included above your latest posts on the home page.

Instead of entering many lines of content via this Home Page Intro box,  you could add the content to a file and then “include” it by entering the 1 line of code that will do this in your index.php file (via Appearance – Editor – index.php ).

Please keep in mind that not all themes are set up this way. With any theme you can design and create your home page and then, via the settings, choose that page as your home page if you want a static front page (i.e. without the latest posts being displayed). But this coding option is handy if you want to add some static content to the otherwise dynamic home page that displays your latest posts.

Adding static information to a file via the editor can be tricky if you are not tech savvy or have little programming knowledge. So, including a file instead tends to be easier.

Steps for an Include File

  1. Create a php page with your required content.
  2. Save the page and upload it to the root directory of your website.
    • In this example, I am entering home page content in the file to be included.  Thus the name of the file reflects its purposes.
  3. Enter the following line of code where you would like this content to appear.
    • In my example, I want it to appear on the home page (index.php), above the most recent posts.
<?php include ‘http://www.YourDomainName.com/home-content.php’; ?>

 Once you are happy with the output, you can rest easy knowing that any future changes simply have to be added to the included php file. You don’t have to go back and edit the index.PHP file.

What is the Difference Between Require and Include?

 You can replace the word include with require  if you prefer.

  • Include – The main difference is that if there is a problem with the file being found, in other words, an error, then the rest of the page will still load.
  • Require – If you use require instead, and there is an error in finding the file, then the rest of the script is halted.

~~~~~~~~~~~~~~~~~~~

 Include can be very handy, as mentioned, for menus if you are creating a website from scratch. If you are using WordPress, it can be handy for adding static content to any of your dynamic pages.

 

 Teaser Image courtesy of Stuart Miles, FreeDigitalPhotos.net


Leave a comment

Your email address will not be published. Required fields are marked *

CommentLuv badge

One thought on “Tip of The Day: How Do You Include HTML in PHP?