$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display post metadata: "title, category, author, date" with shortcode|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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

Post a comment

comment list (0)

  1. No comments so far