I need to output the text before the_category.

In result, it should be something like

Posted on September 20, 2014 IN MyCategory

Here I use the_time('F j, Y') and start from here I use the_category().

Word "in" should be inside the_category and before words "MyCategory".

For example, for the_date I can use $before to place text before the_date.

Here I need to do the same for the_category(), but I can't get this to work.

Any suggestions on how to solve this issue?


2 Answers 2


I don't see why you can not use plain text like tho

Posted on <?php the_time('F j, Y') ?> in <?php the_category(); ?>

If what you are asking is really important, just wrap the function like this

echo $before;
$echo $after;

If the_category is not picking the post ID, then pass it manually.

  • It didn't help.
    – Aineko
    Oct 11, 2014 at 1:43
  • @Aineko "It didn't help" is not a proper reply. Explain in short why doesn't the answer help, what is not happening or what happens that should not. The user that posted the answer are unable to help you with a reply like the one you've posted Oct 11, 2014 at 10:28

You can create custom function for displaying any kind of terms.

By terms I mean categories and tags, and by any kind I mean both regular and custom post types terms.

In that function you can add some kind of $text_before parameter, which you can use to add text before your category list, which will be displayed conditionally, only if your function is returning something, which I am assuming you want to achieve - avoid displaying "before" text if there are no categories selected for the post item. Am I right?

Function could look like this:

function mytheme_get_terms ($postID, $term, $text_before = "") {

    $terms_list = wp_get_post_terms($postID, $term);
    $output = '';

    $i = 0;
    foreach( $terms_list as $term) {
        if($i == 1){$output .= $prefix;}
        if($i > 1){$output .= ', ';}
        $output .= '<a href="' . get_term_link($term) . '">'. $term->name .'</a>';

    return $output;

and you can call it like this:

<?php echo mytheme_get_terms($post->ID, 'category', 'IN'); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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