WordPress shortcodes allow you to save time. For example, on my WooCommerce product pages I found myself typing the product title at least three times: in the product name input field, ok, but also again in the product long description and in a custom product tab inside a text link.
So, while finding ways to save time and reduce errors and typos, I coded the shortcode below so that I could avoid writing the title 3 times (and they’re very long!).
How does it work? Simply use [post_title] in a WooCommerce product, WordPress page, WordPress post or custom post and this will magically turn into the current post title. Simple!
PHP Snippet: Print Current WooCommerce Product / WordPress Post Title Via a Shortcode
Yep, it’s only one line of PHP magic!
/**
* @snippet Echo Current Product/Post Name (Shortcode)
* @usage Use [post_title] on the current post
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 8
* @community https://businessbloomer.com/club/
*/
add_shortcode( 'post_title','get_the_title' );
Very useful as always. Thank you Rodolfo.
hello, i pasted code given by about “WooCommerce: Product Title Shortcode”
in mobile mode im using page title as shortcode on header but some page title have more length of words how i can limits page title length by words? please help me.
Hello Alan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
What file does the PHP code snippet go in?
Hello Mark, sorry for the delay! Custom code goes preferably into functions.php of your child theme – or in a Code Snippet plugin snippet. Let me know