0

I'm trying to add the category to the post-meta.php file so the blog page will not only show the author and date, but also the category for each post. However, I can't seem to find any correct way to do so. The current code is the following:

<span class="post-meta">
<?php
$author = "<a href='" . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . "'>" . esc_html( get_the_author() ) . "</a>";
$date   = "<a href='" . esc_url( get_month_link( get_the_date( 'Y' ), get_the_date( 'n' ) ) ) . "'>" . date_i18n( get_option( 'date_format' ), strtotime( get_the_date( 'r' ) ) ) . "</a>";
?>
<?php printf( _x( 'Published by %1$s on %2$s', 'This blog post was published by some author on some date', 'author' ), $author, $date ); ?>

Could someone please help me do this? Thanks!

  • you just want to get and show the category of a post? – David Lee Aug 17 '17 at 2:39
1

you can use get_the_category_list() it will show a list of the categories of the post, you can also use get_the_category() it will return an array, you can iterate it to format it, you can find examples here.

0

<?php foreach((get_the_category()) as $category) { ?> <a href="<?php echo $category->name; ?>"><?php echo $category->name; ?></a>

use echo $category->name to get category name Or use echo $category->category_nicename to get class name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.