10 WordPress Template Tags You May Not Know

WordPress is shipped with utterly an contentment of Template Tags given a inception. These Template Tags in WordPress are PHP functions that can be used to output as good as retrieve a square of data.

If we have been building a WordPress theme, we competence be informed with some of these Template Tags, such as the_title that shows a post title, the_author that shows a name of a post’s author, and a couple of a post.

How to Create Custom WordPress Template Tags

How to Create Custom WordPress Template Tags

Building a WordPress Theme is so easy with template tags. You can supplement the_title() to arrangement a post…

Read more

WordPress keeps evolving. Every new recover mostly introduces a few new Template Tags. So most so that gripping adult with all these Template Tags — aged or new — can be utterly challenging. Check out a these 20 template tags we competence have overlooked.

Capital P

WordPress, as per their guideline and standard, has to be created with a capital P i.e. WordPress is a no no; a right approach is to spell it as WordPress.

The collateral “P” is an emanate with so most significance to a extend that Matt Mullenweg (founder of WordPress) has enclosed it in his fortitude behind in 2009. The capital_p_dangit() duty is introduced as partial of a initiative.

Since: 3.0.0

// Using it straightforwardly
$footer_text = get_theme_mod( "footer_text", "" );
$footer_text = captial_p_dangit( $footer_text ); // Any WordPress calm is incited with collateral P.

// Or, regulating it in a WordPress Filter.
add_filter( "the_excerpt", function( $text ) {
 	return captial_p_dangit( $text );
} );
Custom Logo

In 4.5, WordPress introduced a ability to upload a logo for themes by a Customizer. This new underline requires thesis support: by adding add_theme_support( 'site-logo' ), a trademark will seem in a Customizer.

This underline leads to a use of a few new Template Tags that can hoop a trademark picture outlay on Themes, namely: has_custom_logo(), get_custom_logo(), and the_custom_logo().

Since: 4.5.0

// 1. Output includes a picture trademark and a couple behind to home.
the_custom_logo();

// 2. Get a tradition trademark outlay "string".
$logo = get_custom_logo();

// 3. Conditional
if ( has_custom_logo() ) {
	$logo = get_custom_logo();
}

// 4. Using a 'get_custom_logo' to hang a trademark with a div;
add_filter( "get_custom_logo", function( $html ) {
	return '';
} );
Thumbnail Image URL

WordPress has a long-integrated, local application to supplement a thumbnail picture or featured image. The Template Tag, the_post_thumbnail(), shows a picture tab along with their attributes.

But what if we wish to uncover a picture thumbnail as a credentials by CSS instead? Use a Template Tag, get_the_post_thumbnail_url().

Since: 4.4.0

Example:

?php relate get_the_post_thumbnail_url(); // e.g. "http://example.com/path/to/image/thumbnail.jpg"?

div class="image-thumbnail" style="background-image; url(?php relate get_the_post_thumbnail_url() ?)"/div
Generate Random Number

This Template Tag will give we a pointless series formed on a specified range. WordPress is regulating this duty internally to beget a pointless password. You can substantially use it to beget a pointless banking series for your WooCommerce site.

Since: 2.6.2

Example:

// Generate a series from 1 to 200
$rand_number = wp_rand( 1, 200 ); // outlay will not be next 0 or 201 above.
Comments Pagination

Most Themes are now regulating a the_comments_navigation() that will give a “Next” and “Prev” form of navigation link. If we wish to uncover a numbered navigation (pagination), reinstate a tab with the_comments_pagination() instead.

Bear in mind that a Template Tag is usually accessible in WordPress 4.4.0 upwards. Make certain to run a check before deploying it.

Since: 4.4.0

Example:

?php
// Replace a `the_comments_navigation()`
if ( function_exists( 'the_comments_pagination' ) ) {
	the_comments_pagination();
} else {
	the_comments_navigation();
}

ol class="comment-list"
	?php
		wp_list_comments( array(
			'style'       = 'ol',
			'short_ping'  = true,
			'avatar_size' = 42,
		) );
	?
/ol

!-- .comment-list --
Shortening URL

This Template Tag will digest a url length. And such a really prolonged URL won’t mangle into a new line within a physique content. There are 2 options we can take: supplement overflow-wrap: break-word; in your CSS, or trim a length of a URL with a url_shorten() Template Tag.

Since: 1.2.0

Example:

$link = get_the_permalink();
$url_text = url_shorten( $link ); // e.g. www.hongkiat.com/blog/css...
echo 'a href="'. $link .'"'. $url_text .'/a';
Add Inline Scripts

We have always used a wp_enqueue_script to register, bucket a book and a dependencies. Loading an inner script however was not utterly straightforward, until this Template Tag, wp_add_inline_script is introduced.

Adding an inline book requires a famous enqueued book to that it will be attached. This handler is upheld as a initial parameter of a book identical to a wp_localize_script() function. The second parameter should pass a calm of a script. The third parameters specifiy either a inline should be outlay ‘before’ or ‘after’ .

Since: 4.5.0

Example:

function enqueue_script() {
   wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20160412', loyal );
	wp_add_inline_script( 'twentysixteen-script', 'window.hkdc = {}', 'before' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_script' );

// Output:
// book type='text/javascript'window.hkdc = {}/script
// book type='text/javascript' src='http://local.wordpress.dev/wp-content/themes/twentysixteen/js/functions.js?ver=20160412'/script
Dropdown Language

The wp_dropdown_languages Template Tag will outlay an HTML choice arrangement a list of languages in your WordPress site. You will find this template tab useful if we need to focus your website. You can use it to uncover your denunciation options in a User Editor shade or in a front-end of your site to concede users to name their denunciation preference.

List options of accessible denunciation in User setting.

Since: 4.0.0

Example:

wp_dropdown_languages( array(
    'id' = 'lang_options',
    'name' = 'lang_options',
    'languages' = get_available_languages(),
    'translations' = array( 'id_ID', 'ja' ), // Indonesia, and Japan
    'selected' = 'en_US',
    'show_available_translations' = false,
    )
);
Get a Avatar Image URL

As a name suggests, this Template Tag, get_avatar_url(), will retrieve a picture trail of a user’s avatar. It allows we to arrangement and mold a avatar in any approach we like, instead of simply displaying it by a HTML picture tag.

Since: 4.2.0

Example:

$avatar = get_avatar_url( 'admin@domain.com' );
div class="avatar-url" style="background-image: url(?php relate $avatar; ?)"
/div
Get Theme

This duty retrieves an intent containing information of a now active Theme. This information includes a thesis Slug, Name, Version, Text Domain, Author, etc.

In a following formula snippet, we use it to collect a chronicle and pass it as a book version.

Since: 3.4.0

Example:

$theme = wp_get_theme();
define( 'THEME_SLUG', $theme-template ); //twentysixteen
define( 'THEME_NAME', $theme-get( 'Name' ) ); // Twenty Sixteen
define( 'THEME_VERSION', $theme-get( 'Version' ) ); //1.2

function load_scripts() {
	wp_enqueue_script( 'script-ie', $templateuri .'js/ie.js', array( "jquery" ), THEME_VERSION );
	wp_script_add_data( 'script-ie', 'conditional', 'lt IE 9' );
}
add_action( 'wp_enqueue_scripts', 'load_scripts' );

Add Comment