Post Title, Date and Author Shortcodes
Adds [post_title], [post_date], [post_modified], [post_author] and [post_url] shortcodes for templates, patterns and widgets that need details of the current post.
Opens WPIDE on
Code
add_shortcode( 'post_title', function () {
return esc_html( get_the_title() );
} );
add_shortcode( 'post_date', function ( $atts ) {
$atts = shortcode_atts( array( 'format' => '' ), $atts, 'post_date' );
return esc_html( get_the_date( $atts['format'] ) );
} );
add_shortcode( 'post_modified', function ( $atts ) {
$atts = shortcode_atts( array( 'format' => '' ), $atts, 'post_modified' );
return esc_html( get_the_modified_date( $atts['format'] ) );
} );
add_shortcode( 'post_author', function () {
$post = get_post();
return $post ? esc_html( get_the_author_meta( 'display_name', $post->post_author ) ) : '';
} );
add_shortcode( 'post_url', function () {
return esc_url( get_permalink() );
} );
Notes
Date shortcodes accept a format, for example [post_date format="j F Y"].
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.