One of many best options of WordPress is Shortcodes. The flexibleness to program dynamic content material utilizing shortcodes may also help you to deploy strong and unimaginable websites. Shortcodes are mainly substitution strings you can insert into your content material that renders dynamic content material.
I’m serving to a consumer this week the place they’re taking one in every of their merchandise and rolling it out into a brand new area. The positioning is lots 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 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 12 months and subtracts it from the 12 months the corporate was established. We will place a default 12 months to all the time calculate from OR we are able to cross the 12 months. We will register the shortcode by including this operate to the positioning’s theme’s capabilities.php file.
If you happen to’re superior, you could need 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:
operate yearssince_shortcode($atts) {
$atts = shortcode_atts(array(
'startdate' => '7/14/2005',
),
$atts
);
$startdate = new DateTime($atts['startdate']);
$in the present day = new DateTime(date('m/d/Y'));
$datediff = $today->diff($startdate);
$yeardiff = $datediff->y;
return $yeardiff;
}
add_shortcode( 'yearssince', 'yearssince_shortcode' );
What the operate does is subtract the present 12 months from the 12 months specified, or use the date you enter on this code because the default. On this case, I used the date of the primary printed put up 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!
After all, you will get way more complicated with this kind of shortcode… you may use HTML, photos, 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.