I want to add a category variable along with the date variable that post has been published.
For example, when I go to a post, I see the following under the post title:
last updated on May 10, 2019
I want to add the category name that this article has been posted to. For example, if I add this post under news category, it will show the category along with date as a link for this category:
last updated on May 10, 2019 in news
So far I know the code that we should modify in templates-tags.php file especially:
printf(
'<span class="posted-on">last updated on %1$s</span><span class="byline"> <i class="fa fa-user"></i> %2$s</span> in ',
sprintf(
'<a href="%1$s" rel="bookmark">%2$s</a>',
esc_url( get_permalink() ),
$time_string
),
sprintf(
'<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_html( get_the_author() )
)
);
}
endif;
I want to add a category variable along with the date variable that post has been published.
For example, when I go to a post, I see the following under the post title:
last updated on May 10, 2019
I want to add the category name that this article has been posted to. For example, if I add this post under news category, it will show the category along with date as a link for this category:
last updated on May 10, 2019 in news
So far I know the code that we should modify in templates-tags.php file especially:
printf(
'<span class="posted-on">last updated on %1$s</span><span class="byline"> <i class="fa fa-user"></i> %2$s</span> in ',
sprintf(
'<a href="%1$s" rel="bookmark">%2$s</a>',
esc_url( get_permalink() ),
$time_string
),
sprintf(
'<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_html( get_the_author() )
)
);
}
endif;
Share
Improve this question
edited May 20, 2019 at 0:04
butlerblog
5,1313 gold badges28 silver badges44 bronze badges
asked May 19, 2019 at 23:10
iLinux85iLinux85
1274 bronze badges
1 Answer
Reset to default 0You can use get_the_category_list()
, which returns a list of categories assigned to the post (each category in the list links to the category archive):
printf(
'<span class="posted-on">last updated on %1$s</span><span class="byline"> <i class="fa fa-user"></i> %2$s</span> in %3$s',
sprintf(
'<a href="%1$s" rel="bookmark">%2$s</a>',
esc_url( get_permalink() ),
$time_string
),
sprintf(
'<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_html( get_the_author() )
),
get_the_category_list( ', ' )
);