WordPress.org

Ready to get started?Download WordPress

Forums

Why does PHP call to the_category() do this? (4 posts)

  1. carlos123
    Member
    Posted 5 years ago #

    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.

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

    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.

    code
    echo 'Posted in '.the_category(', ');
    echo ' | ';
    /code

    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.

    Thanks.

    Carlos

    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.

  2. carlos123
    Member
    Posted 5 years ago #

    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.

    Anybody?

    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.

    Carlos

  3. carlos123
    Member
    Posted 5 years ago #

    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.

    Hmmm...

    Carlos

  4. willkemp
    Member
    Posted 5 years ago #

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

    or:

    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".

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.