To display the post title from a shortcode in the frontend of a WordPress site , you can use the following code:
Define the shortcode function in your theme’s functions.php file:
php
function post_title_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'post_title' );
$post_title = get_the_title( $atts['id'] );
return $post_title;
}
add_shortcode( 'post_title', 'post_title_shortcode' );
- Add the shortcode
[post_title]
to the content of any page or post where you want to display the post title.
For example, if you want to display the title of the current post, you can use the shortcode like this:
The title of this post is: [post_title]
If you display the title of the specific post. you can use the short code like this.
The title of post with ID 123 is: [post_title id="123"]
When the page or post is viewed on the frontend, the shortcode will be replaced with the post title.