What are Template Tags in WordPress
When you are developing template from scratch or using tags to retrieve data with in theme from database template tags are used. Template tags are used with themes to retrieve content from your database by pull methods. In other words template tags which gets something from database, can also be called as below:
1. A Simple PHP code Tag.
2. A WordPress Function.
3. Optional Parameter.
When you are working from WordPress, PHP functions are used in your WordPress theme templates to display Loop content are called template tags. These tags are used to display and show detailed or specific pieces of data about your website and its content which lets you to alter how you want to customize and where you want to display content on your website.
Why to Use Template Tags:
The main use of template tags is it allows developer very easy to include various pieces of template in a theme file which eases to maintain the theme very easily and also help developer not to change bulk of changes. For example, if you create a header.php file in WordPress it will also include single.php, page.php, front-page.php and all other files and in it and will be referencing it to
get_header().; rather than copying all code in to each file. More over when you make changes to header.php file all the changes made will be carried out to the rest of the files in which it is declared.
Other use of template tags is to display dynamic data in your header.php file and include the title as so you want to display by declaring
Website Blog title.
Doing above is by manually adding title by editing your theme file when ever you want to make changes to your blog of your website. So its easier for developers to include the template tag
. Now you can make changes in WordPress by declaring it from back end instead of hardcoding in template file.
How to use Template Tags in WordPress
You can use template tags by simple adding the template tags like by adding php code
get_footer(); in your theme files when ever you want and it prints the declared template tags.
How and Use Template Tags in WordPress Loops:
To learn how template tags work, just place any template tag inside the Loop and view the results. The following example views the values of a couple different template tags:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <br /> <?php the_content(); endwhile; endif; ?>
By Above example, your post titles are displayed with links to the permalink for each post. The content of the post is displayed directly below the post title.
Template tags which needs to be inside the loops are :
Examples of Template Tags in WordPress:
For simple understanding lets take an example of
the_title() template tag which displays the title of your post or page inside the Loop on your blog.
The template tag
get_header() tells WordPress to get the header.php file and include it in the current theme file.
The template tag
get_footer() tells WordPress to get the footer.php file.
Advantages and Benefits of Using Template Tags in WordPress:
The major advantage or benefit of using template tags is that you don’t need to know PHP code to use them and you can customize and display what ever you want on your website by using template tags in WordPress.
There are many different template tags are available in WordPress in which few of the template tags must be used inside the loop whereas other tags can be used anywhere in your theme template files.
List of Commonly Used Template Tags
Ther is list of handful template tags available in wordpress which you can use in your loops below are the most commonly used tempalte tags available in te loop, these template tags will return and display the post data listed.
the_permalink() is used to displays the URL of your post.
the_title() is used to displays the title of the post.
bloginfo(‘name’) which tells WordPress to get the blog title
the_ID() is used to displays the unique ID of your post.
the_content() is used displays the full content of your post.
the_excerpt() is used to displays the excerpt of your post.
If the Excerpt field is filled out on the Post edit screen, that will be used.
the_time() is used to displays the date/time your post was published.
the_author() is used to displays the author of the post.
the_tags() is used to displays the tags attached to the post.
the_category() is used to displays the categories assigned to the post.
edit_post_link() is used to displays an edit link that is shown only if you are logged in and allowed to edit the post.
comments_popup_link() is used to displays a link to the comments form of your post.