Monday, September 12, 2022
HomeMobile MarketingCease Updating Years On Your WordPress Web site With This Shortcode To...

Cease Updating Years On Your WordPress Web site 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 which you can insert into your content material that renders dynamic content material.

I’m serving to a shopper this week the place they’re taking one among their merchandise and rolling it out into a brand new area. The location is tons of of pages and has been fairly an enterprise. As we’ve been engaged on the hit record 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 have been correct at 17… all relying upon after they have 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 move the yr. We will register the shortcode by including this perform to the location’s theme’s capabilities.php file.

For those who’re superior, it’s possible you’ll wish to construct a customized plugin in 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 the moment = 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 instance, 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 may get much more complicated with this sort of shortcode… you could possibly use HTML, photographs, CSS, and so forth. 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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments