WordPress.org

Ready to get started?Download WordPress

Forums

WordPress global variables (12 posts)

  1. fterh
    Member
    Posted 4 years ago #

    Hello,

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

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    There's a list here i find quite useful.
    ifacethoughts.net/2006/02/25/wordpress-global-variables/

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

    <pre>
    <?php print_r( get_defined_vars() ); ?>
    </pre>
  3. fterh
    Member
    Posted 4 years ago #

    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.

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  5. fterh
    Member
    Posted 4 years ago #

    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

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    See here.
    http://codex.wordpress.org/Template_Tags/wp_list_comments#Comments_Only_With_A_Custom_Comment_Display

    More about variables and their scope.
    http://uk3.php.net/manual/en/language.variables.scope.php

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

  7. fterh
    Member
    Posted 4 years ago #

    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)?

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  9. fterh
    Member
    Posted 4 years ago #

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

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.
    http://codex.wordpress.org/Theme_Development#Theme_Functions_File

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

  11. fterh
    Member
    Posted 4 years ago #

    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 "

    ";
    print_r(get_defined_vars());
    echo "
    ";
    echo "
    ";
    comment_text();
    }

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

  12. fterh
    Member
    Posted 4 years ago #

    Bump

Topic Closed

This topic has been closed to new replies.

About this Topic