Support » Everything else WordPress » Why does PHP call to the_category() do this?

  • Hi there,

    This is my first post here. I tried posting this in WP-Advanced but couldn’t so not knowing where else to post this…I came to this forum.

    I am making changes to the index.php file inside the default theme (a copy of the theme actually) trying to quickly get up to speed on how to create my own themes.

    So far so good but I got a question I am stumped on that I would appreciate input on.

    The following code works just fine.

    echo ‘Posted in ‘;
    echo the_category(‘, ‘);
    echo ‘ | ‘;

    But for some strange reason that I can’t for the life of me figure out the following causes what the_category() returns to be echoed before ‘Posted in ‘ is.

    echo ‘Posted in ‘.the_category(‘, ‘);
    echo ‘ | ‘;

    Why is that? Does the_category() not return just a string? If so…why does the above code (the second example) spit out a messed up result? Is WP doing some pre-execution interpretation of the PHP code?

    Any input would be most appreciated.



    PS. I appreciate the way WP tries to make things dummy proof but it’s frustrating trying to get around the dummy proofing and have things work as one might expect. A case in point being how WP uses backticks inside forum posts instead of the usual block BB code. I tried putting “code” in backticks and it just spits out the word “code” instead of puttign my code into a quote…sigh.

Viewing 3 replies - 1 through 3 (of 3 total)
  • It does appear that WP is doing something strange with the return values of functions like the_category().

    If I assign the return value to a variable and then echo it…the output is likewise messed up.

    If I do an “echo the_category(‘, ‘);” no problem. Weird.

    I sure hope WP does not do any pre-processing because that’s going to throw a real kink in my efforts to use WP as a CMS.


    Hmm…maybe the_category() doesn’t return anything and instead prints out something to the screen. Hmm…that might be it.

    “echo the_category()” might work and spit out the correct thing to the screen because it’s in the right place in the code (essentially the echo part is ignored). Whereas if I move it elsewhere it spits out it’s thing there instead. Hmm…I think that might be it.


    I think it might be a PHP thing not a WP at all.

    PHP might evaluate and call functions in the code first before evaluating the code around a function and executing it which might explain why concatenating the_category() to a string doesn’t work as expected.

    What the_category() spits out on it’s own gets output before the string I am concatenating it to.



    The problem is that the_category() outputs its results directly to the browser – it doesn’t return them. That means when you do

    echo the_category();

    you’re echoing nothing, and the_category() is directly outputting its results.

    The proper way to do what you want to do is:

    echo 'Posted in ';
    the_category(', ');
    echo ' | ';


    echo 'Posted in '.get_the_category().' |';

    WordPress functions that output directly to the browser generally (always?) come with a version that’s prefixed “get_” which returns a string, rather than outputting it. The returned result can then be manipulated as a string (which means it can also be echoed).

    By the way, the proper forum for this is “Plugins and Hacks”.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Why does PHP call to the_category() do this?’ is closed to new replies.