Display post metadata: "title, category, author, date" with shortcode

admin2025-04-17  19

How to create shortcodes to the standard post-metadata "title, author, category and date" in order to display it in post-content?

E.g. post-content including shorcodes: Lorem ipsum dolor [post_title] sit amet, [post_category] consectetur adipiscing elit [post_author]..

Followed this guide and it worked for the title but can't make it work on the other metadata: category, author name and date. The code is the following:

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

How to create shortcodes to the standard post-metadata "title, author, category and date" in order to display it in post-content?

E.g. post-content including shorcodes: Lorem ipsum dolor [post_title] sit amet, [post_category] consectetur adipiscing elit [post_author]..

Followed this guide and it worked for the title but can't make it work on the other metadata: category, author name and date. The code is the following:

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

Share Improve this question asked Jun 20, 2018 at 14:51 MathiasMathias 351 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

If you are outside the loop then you can use to get them by post id, you can play around with these snippet:

shortcode for author's name:

function author_name_shortcode(){
    global $post;
    $post_id = $post->ID;
    $author = get_the_author($post_id);
    return $author;
}
add_shortcode('post_author','author_name_shortcode');

shortcode for categories name:

function category_name_shortcode(){
    global $post;
    $post_id = $post->ID;
    $catName = "";
    foreach((get_the_category($post_id)) as $category){
        $catName .= $category->name . " ,";
    }
    return $catName;
}
add_shortcode('post_category','category_name_shortcode');

This is usually inside your theme. For post options you can decide which meta data you'd like displayed. If your theme doesn't offer these options then you might consider using a more extensive theme like Genesis

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744874128a271863.html

最新回复(0)