Skip to content

Instantly share code, notes, and snippets.

@decodist
Last active July 7, 2020 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save decodist/1a2c38637484181c4e510be0af20039f to your computer and use it in GitHub Desktop.
Save decodist/1a2c38637484181c4e510be0af20039f to your computer and use it in GitHub Desktop.
Add author info to WordPress blog post using a shortcode
<?php
//add any of the following shortcode functions to your theme's functions.php file
//display the author name which is linked to the About page
function author_name_shortcode(){
global $post;
$post_id = $post->ID;
$author = get_the_author($post_id);
$linkedAuthor = "<a href='/about'>".$author."</a>";
return $linkedAuthor;
}
add_shortcode('post_author','author_name_shortcode');
//display the post's last modified date
function post_updated_shortcode(){
global $post;
$post_id = $post->ID;
$mod_date = get_the_modified_date('F jS, Y', $post_id);
return $mod_date;
}
add_shortcode('post_updated','post_updated_shortcode');
//display the author AND last modified date
function post_credits_shortcode(){
global $post;
$post_id = $post->ID;
$mod_date = get_the_modified_date('F jS, Y', $post_id);
$author = get_the_author($post_id);
$linkedAuthor = "<a href='/about'>".$author."</a>";
$post_creds = $linkedAuthor." | ".$mod_date;
return $post_creds;
}
add_shortcode('post_credits','post_credits_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment