Construction a WordPress Theme is really easy with template tags. You’ll upload the_title() to show the publish or web page name, and you’ll use the_content() to show publish or web page contents. There are lots of extra template tags in WordPress that we will be able to use to show different issues.

However relating to exhibiting best explicit issues in your theme, you could wish to create your personal template tag. In these days’s educational, we’re going to stroll you via this not-too-complicated procedure. Let’s get began with the basics.

Elementary Template Tag

Should you check out the WordPress Core sources, you’re going to in finding {that a} template tag is principally a PHP serve as working a suite of codes with some parameters.

To create your personal template tag, you’ll write a PHP serve as in purposes.php inside your theme listing, for instance.

serve as my_template_tag() {
	echo 'That is my template';
}

Then, for your different theme information, say, unmarried.php or web page.php, you’ll upload the serve as, like so.


This may occasionally show the ‘That is my template’ this is echoed throughout the my_template_tag() serve as.

basic template tagbasic template tag

Making a WordPress Template is in point of fact that easy. Alright, allow us to now check out a quite extra complex instance.

Web page View Rely Template Tag

On this instance, we’re going to create a template that can display view depend for every publish and web page.

First, set up and turn on WordPress.com Stat in Jetpack. And just be sure you have hooked up Jetpack to WordPress.com. The rationale we use WordPress.com Stat is that the View knowledge shall be saved in WordPress.com somewhat than in our personal database. This would save on our server load.

Create a serve as for your purposes.php named the_view(), like so:

serve as the_view() { 

}	

We can put the serve as (the template tag) in a web page or publish, so we wish to get the web page and publish ID quantity. This may also be retrieved the usage of get_the_ID();.

serve as the_view() { 
	$identification = get_the_ID();
}	

We will be able to then use a serve as to retrieve the collection of perspectives from WordPress.com Stats through the usage of stats_get_csv();. This serve as accepts a number of parameters ( in finding all the listing here).

In our case, we wish to upload the days, which specify the time vary of the view depend, and the post_id.

serve as the_view() {
	$identification = get_the_ID();
	$page_view = stats_get_csv('postviews', 'days=-1&post_id='.$identification.'');
}

Within the code above, since we set the days parameter to -1, we will be able to retrieve the view depend of the given publish ID from the very starting, from when the WordPress.com Stat plugin is activated.

After we’ve the quantity, we simply wish to echo it, as follows.

serve as the_view() {
	$identification = get_the_ID();
	$page_view = stats_get_csv('postviews', 'days=-1&post_id='.$identification.'');
	echo $page_view[0]['views'];
}

That’s it, our new template tag for exhibiting web page view depend is finished. You’ll any place in web page.php or unmarried.php. As an example:

post count viewpost count view

You probably have any questions, please be happy to place them within the remark phase beneath.

The publish How to Create Custom WordPress Template Tags gave the impression first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/wordpress-custom-template-tags/

[ continue ]