Support » Fixing WordPress » WordPress global variables

  • Hello,

    Is there a page listing WordPress’s global variables like $comments (which I use in a foreach statement)?

Viewing 11 replies - 1 through 11 (of 11 total)
  • There’s a list here i find quite useful.

    You can also print the defined varaiables in PHP by doing something like the following..

    <?php print_r( get_defined_vars() ); ?>

    Thanks for the speedy reply. Here’s another question.

    I want to make my own comments structure like <author>, on <date & time> says:

    I know I have to use the foreach statement: <?php foreach($comments as $comment) : ?> which works perfectly but I’m just curious: In the link you sent me, $comment is a global variable but $comments is not. However in the PHP foreach statement if I replace $comment with $comment2 it works fine, but not if I replace $comments with $comments2.

    So I’m just kind of confused as to which is the real global variable.

    Print them out, if the variable is global it’ll contain data, if not then it won’t contain anything unless you’ve placed data into the given variable, be it $foo, $bar, or whatever variable..

    I tried using the print_r function, I realised that $comment is not a global variable but $comments is. Also they have to be in the loop (I think) because I made a new php file in the same directory with the code:

    <?php print_r($comments); ?>

    which doesn’t work.

    Are my deductions correct about $comments and not $comment being the global variable and they have to be in the loop?

    Edit: Also how do you use for foreach loop to display x number of comments on each comments page, where x is the number defined under Settings>Discussion

    See here.

    More about variables and their scope.

    As i said before, you can print out all the defined variables with get_defined_vars(), it’ll be a fairly big list, so be prepared to scroll…

    I printed them out but the list was very short.

    Wait, I printed it on another php file – maybe that’s why. Yep, it worked when I used it inside the loop.

    Last question. I noticed my functions.php file is totally redundant. I deleted everything and saved, and my page looks perfectly fine. I pasted back everything and refreshed, and there is no change.

    So should I just remove everything inside functions.php file and make my own function (one only)?

    It’s not a required file, you only need to have one if you wish to run additional or custom functions.

    I tend to do most of my customizations that way, save having a plugin for every little thing.

    Mm so you mean normally the functions file has a set of functions, and theme designers only call those functions when needed?

    The file does not need to exist, but you may want it if you wish to add functions, such as a comments callback.

    It’s there for when and if you want to add functions, it’s not a requirement.

    Take a look at the default theme, or pretty much any other theme, there’s lots of things you can do.

    I did a test. In my comments-template.php:

    <?php foreach($comments as $comment) : ?>
    <?php wp_list_comments(“callback=listComments”); ?>
    <?php endforeach; ?>

    And in the functions.php:

    function listComments($commentz) {
    echo “

    echo "

    echo “

    How come the output is two identical arrays, yet 2 different comment_text()?


Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘WordPress global variables’ is closed to new replies.