0

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;
0

You 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( ', ' )
);
3
  • thank you it actually works! however, there is just two space between the date and in word do you know how to remove just one space between the date and in word?
    – iLinux85
    May 21 '19 at 2:52
  • the temporary solution i did is to make css for the category and with the following css variable settings .category-post { margin-left: -4px; } that eliminates the double space between the date and in word but i don't know if this a good solution or there is something to fix really simple than that but i am not aware about it
    – iLinux85
    May 21 '19 at 3:02
  • Maybe that's more to a CSS issue. If you can share the page URL, I could probably look at the issue - and help fix it in a better/quicker way. Or if you can't share it, then does the issue happen to all posts having the <span class="posted-on">...</span> tag (or element)? Can you inspect the page source/HTML and copy one of those elements? (I mean, copy the HTML markup)
    – Sally CJ
    May 21 '19 at 4:10

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.