admin管理员组文章数量:1130349
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 badges2 Answers
Reset to default 1If 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
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 badges2 Answers
Reset to default 1If 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
本文标签: Display post metadata quottitlecategoryauthordatequot with shortcode
版权声明:本文标题:Display post metadata: "title, category, author, date" with shortcode 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1744874125a2121618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论