Wednesday, September 14, 2022
HomeMobile MarketingCease Updating Years On Your WordPress Website With This Shortcode To Calculate...

Cease Updating Years On Your WordPress Website With This Shortcode To Calculate Years Since


One of many best options of WordPress is Shortcodes. The pliability to program dynamic content material utilizing shortcodes may also help you to deploy strong and unbelievable websites. Shortcodes are principally substitution strings you could insert into your content material that renders dynamic content material.

I’m serving to a shopper this week the place they’re taking considered one of their merchandise and rolling it out into a brand new area. The positioning is tons of of pages and has been fairly an enterprise. As we’ve been engaged on the hit listing of points, one which popped up was that there have been dozens of weblog posts, pages, and calls to motion that spoke to the corporate’s years in enterprise.

Some pages had 13, some 15, others had been correct at 17… all relying upon after they had been written. That is a kind of pointless edits to wish to make {that a} shortcode can deal with completely.

Shortcode for Years Since

All we have to do is register a shortcode that takes the present yr and subtracts it from the yr the corporate was established. We will place a default yr to all the time calculate from OR we will cross the yr. We will register the shortcode by including this perform to the positioning’s theme’s capabilities.php file.

If you happen to’re superior, you might wish to construct a customized plugin to your web site in order that these shortcodes nonetheless work even if you happen to replace to a brand new theme:

perform yearssince_shortcode($atts) {
	$atts = shortcode_atts(array(
		'startdate' => '7/14/2005',
		),
		$atts
	);
	   
	$startdate = new DateTime($atts['startdate']);
	$at this time = new DateTime(date('m/d/Y'));
	$datediff = $today->diff($startdate);
	$yeardiff = $datediff->y;
	return $yeardiff;
 }
 add_shortcode( 'yearssince', 'yearssince_shortcode' );

What the perform does is subtract the present yr from the yr specified, or use the date you enter on this code because the default. On this case, I used the date of the primary printed publish on Martech Zone.

This shortcode will calculate the variety of years since that date. For example, if I want to write how lengthy Martech Zone has been printed, I simply write:

Martech Zone has been printed for over [yearssince] years!

The result’s:

Martech Zone has been printed for over 17 years!

In fact, you will get much more advanced with this sort of shortcode… you would use HTML, photos, CSS, and so on. however that is only a easy instance simply to verify your web site is already correct!

Disclosure: Martech Zone is an affiliate for WordPress and is utilizing an affiliate hyperlink on this article.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments