WordPress Theme Development Tutorial Manual - Template Tags
WordPress Themes Using Template TagsRetrieving content from the database . The content mentioned here can be any data, as small as a blog title or as large as a complete sidebar. Template tags are the preferred method of getting data into the theme, and template tags come in handy:
- Dynamic content can be printed.
- Can be used in multiple theme files.
- Break down the topic into smaller, more understandable parts.
What are template tags?
A template tag is a piece of code that controls the content WordPress fetches from the database, and in terms of composition, template tags can be divided into three parts:
- PHP code tags
- A WordPress Function
- Optional parameters
We can use the template tag toInclude Theme Filesor information in the database.
For example.get_header()
The tag instructs WordPress to get and include header.php
Documentation, ditto get_footer()
The tag instructs WordPress to get and include footer.php
file
In addition to this, there are a number of other types of template tags:
the_title()
: Instructs WordPress to fetch the title from the database and then display it on the page.bloginfo('name')
: Instructs WordPress to fetch the site title from the database and display it on the page.
Notice in the second template tag above, we'll see a parameter in the parentheses that allows us to do two things:
- Specify specific information
- Display the information in the correct format.
Below we'll we'll go over the parameters in more detail, but note that we can send WordPress-specific commands to see how the data is presented.
Why use template tags?
Template tags can encapsulate all the code of a particular content module. This encapsulation allows the encapsulated content to be used in individual theme files and makes the theme easier to maintain.
Create a header.php
file, and then use the get_header() In other theme template files - such as single.php
,page.php
,front-page.php
-in references, it's much easier to maintain than copying and pasting generations of this code in each template file whenever the header.php
When modified, these changes are reflected in every template file that contains him.
Another reason to use template tags is to make it easy to display data from a database. In the title
tag, we can manually include the title
markers, as shown below:
My Personal Website
However, doing so means that we have to edit the template code when we need to change the site title. Instead, use the template tag bloginfo('name') The output tag will come in handy, the tag will automatically fetch and output the site title from the database. After using this tag, I was able to change the title of the site in my WordPress settings without having to modify the template file.
How to use template tags
Template tags are very simple to use, and can be used in any template file with a single line of php code. For example, you can include header.php in your template file with the following code:
get_header().
parameters
Some template tags allow us to pass parameters. Parameters are additional information that determines what content is retrieved from the database.
For example. bloginfo() The template tag allows you to pass in a parameter that tells WordPress what information it needs to get. If you want to output the name of your site, just pass in the "name" parameter, as shown below:
bloginfo( 'name' ).
If you need to get the version of WordPress your site is running, pass in the parameter "version":
bloginfo( 'version' ).
The template tags can have parameters or not, and each template tag has different parameters, which we can use in theWordPress Code Reference Find the list of parameters available for a tag and the actions they can perform on the Template Tags page in the
Using template tags in loops
Many of the template tags must be in the WordPress LoopIn order for them to work, they need to be included in the template file as part of a php "loop" that generates the page the user sees based on the template tags inside.
WordPress loops generally start with the following code:
if ( have_posts() ) :
while ( have_posts() ) .
the_post().
Template tags that work in a loop must be located in the loop to work properly, before the following loop ends:
endwhile.
else :
_e( 'Sorry, no posts matched your criteria.', 'wprs' );
endwhile; else : _e( 'Sorry no posts matched your criteria.
The tags that need to work inside the loop are:
The main reason these functions need to work in a loop is that they require the global post object, which is automatically set by the WordPress loop.
Other tags don't need to work in a loop (like the two below) and can be used anywhere in the template. Such as in the sidebar, in the header or footer.
These tags usually do not require a global post object.