You had it right with categories, just do the same for tags (using get_term_link
instead) – better yet, use the core WordPress functions that automatically generate a list of category/tag links:
function random_post_shortcode( $atts ) {
$posts = get_posts( array(
'numberposts' => 1,
'category_name' => ! empty( $atts['category_name'] ) ? $atts['category_name'] : null,
'orderby' => 'rand',
'post_type' => 'post',
'post_status' => 'publish',
));
if ( ! $posts ) {
return;
}
$post = array_shift( $posts );
$content = apply_filters( 'the_content', $post->post_content );
// get_the_category_list() automatically does what your foreach was doing
// https://developer.wordpress.org/reference/functions/get_the_category_list/
if ( $categories = get_the_category_list( ', ', '', $post->ID ) ) {
$content .= "<br />$categories";
}
// get_the_tag_list() does the same for post tags
// https://developer.wordpress.org/reference/functions/get_the_tag_list/
if ( $tags = get_the_tag_list( '', ', ', '', $post->ID ) ) {
$content .= "<span class="entry-meta" style="font-size: 8px;">$tags</span>";
}
return $content;
}