In this tutorial and screencast, I'll show you how to create a PHP file within a WordPress theme. I’ll also show you have to add some PHP code to the file.
This video is taken from my course Learn PHP for WordPress. You can watch the entire course for free here on Envato Tuts+.
WordPress Theme Files
I’ll start by locating the WordPress theme with all of the template files in it. I’ll then create a new page temple file. The purpose of this is to give an alternative way of displaying some of the pages in a site.
It’s not terribly important what type of file is being created nor what it does. The important thing is how to create PHP file and add relevant code to it.
Create a New File
The way in which you’d do that may be different depending on what code editor you’re using. I’m using Coda for Mac.
Once the file has been created, give it a name. For example demo-page.php.
Coda gives it a little flag to denote the fact it’s a PHP file.
But that alone does not mean it will operate as a PHP file—it needs some code. That means an opening PHP tag: <?php
. There’s no need to add a closing PHP tag as WordPress does not require this.
Add Code to the File
Now we can add some code to the file.
This is a page template file. That means it requires some commented out text at the top to tell WordPress what it is and what it is called.
The first thing to include is a template tag within WordPress to fetch the contents of the header.php file. That’s done by typing get_header
.
Whenever you use a function in PHP, you must always include the braces (brackets) after the function name. This is completed with a semicolon.
The braces indicate that it’s a function and it’s also somewhere that would include any necessary parameters.
No parameters are required in this example.
Add a Loop
Next, add a loop. Again we'll use a template tag.
if (have_posts() ) : while( have_posts() ) : the_post();
The have_posts
template tag lets us know if there are posts to process in the loop. Our loop says, if there are posts to start with, run the the_post()
function as long as there are posts remaining.
To output the content to the page, requires more template tags and some HTML. I’ll show you how to do that in another tutorial.
We finish off our loop using endwhile
and endif
.
Another way of writing this loop is by indenting the if
and while
statements on their own lines with {
curly braces. This style is often easier to read, especially if you have to do more coding inside the loop.
if( have_posts() ) { while( have_posts() ) { the_post(); } }
There are lots of other things you could add to this loop. If there are posts, you might wish to output a header, for example, or a search bar. You can also use an else
statement to handle the case where there are no posts.
Adding Comments
Multiple line comments in PHP have the slash and then the asterisks. You may use just one asterisk or as many as you want.
It’s useful to do this where there is a new block of code.
Ensure that the slashes are at the beginning and the end: /*
and */
. For example, in the functions file, each function has a big section of commented out text and, with the number of asterisks used, it is very visible.
We can also add single line PHP comments by starting the comment with //
.
So I've added an else
statement to the loop, to handle the case when there are no posts. Then I've used //
to add a comment as a placeholder. Here I'm just using the comment as a reminder to myself and to anyone reading the code, what is supposed to happen in the else
statement.
Experimentation
To experiment with a couple more template tags, add get_sidebar
, And get_footer
to the bottom of the file.
Within a theme template file, those comments fetch the sidebar.php and footer.php files. A reason to do this, instead of just adding the code for a sidebar right in your template, is to ensure those pieces of code only need to be written once in the theme. That way, if you change your sidebar for example, you don't have to go back and update every single file in your theme!
With that, you have a bare bones PHP file within the theme. The main elements required are the opening PHP tags, then the PHP template tags and functions. There may be conditional statements as well. As you can see, the code is indented for readability.
If you follow along with the course, In the next lesson, I’ll show you how to add some HTML to your PHP file.
The Best WordPress Themes and Plugins on Envato Market
Explore thousands of the best WordPress themes ever created on ThemeForest and leading WordPress plugins on CodeCanyon. Purchase these high-quality WordPress themes and plugins, and improve your website experience for you and your visitors.
Here are a few of the best-selling and up-and-coming WordPress themes and plugins available for 2020.
-
Inspiration15+ Best WordPress Portfolio Themes for Creatives
-
WordPress17 Best WordPress Slider & Carousel Plugins of 2020
-
WordPress20 Best WordPress Calendar Plugins and Widgets
-
WordPress Themes23+ Best Responsive WordPress Themes (For Sites in 2020)
-
WordPress24 Best WPBakery Page Builder (Visual Composer) Addons & Plugins of 2020
-
WordPress Themes20+ Best Coaching & Consulting WordPress Themes (2020)
No comments:
Post a Comment