Disclosure: This post contains affiliate links. If you click through and make a purchase, I’ll earn a commission, at no additional cost to you. Read my full disclosure here.

How to convert a post title in to a shortcode

The GeneratePress Block Elements, which are part of GeneratePress Premium, are missing two crucial features: Template tags and the ability to use a post’s featured image as a container’s background. While there isn’t an easy solution for the latter, the former can be circumvented using a code snippet which allows you to insert a post’s title as a shortcode anywhere on your sites, in Block Elements, and also in GenerateBlocks Headlines.

The official template tags allow you to use the post title and publish date, among others, in Header Elements, making them perfect for the design of intricate page heroes. You can display either of the mentioned options using {{post_title}} or {{post_date}}.

Until the ability to add these template tags is added to GeneratePress Premium, which will most likely happen sooner rather than later, there is a workaround anyone can use. Read on to see how it’s done.

A snippet that turns a title into a shortcode

Adding the following snippet to WordPress using the frequently used Code Snippets plugin, will turn any page or post title in to a shortcode you can place anywhere using the block editor. Just insert the shortcode [page_title] or [post_title] using a shortcode block.

function post_title(){
return get_the_title();
}
add_shortcode( 'page_title', 'post_title' );

You can use the shortcode in Block Elements or, as I do, in reusable blocks. Of course, you can also manually add it to any post or page.

Post title in a GenerateBlocks Headline

For GeneratePress and GenerateBlocks users, one of the main uses of this snippet will be the ability to add a post’s title to a GenerateBlocks Headline. To do so, simply create your GenerateBlocks Headline and enter the shortcode. It can even be added in a sentence, as shown in the screenshot below.

A shortcode in a GenerateBlocks Headline

While you won’t be able to see it on the backend, as soon as you view the article, the shortcode will be replaced with the appropriate title.

A post's title being displayed in a GenerateBlocks Headline

About Liam Alexander Colman

Liam Alexander Colman has been working with GeneratePress for over two years and has built countless WordPress websites using it. In his personal setting, Liam maintains six websites. While not a developer, he has created innumerable GeneratePress Elements and is well versed in customizing the theme to his liking. Visit this link for more information and a showcase of his work.

Leave a comment