Forums

Beginner's Question Regarding Code with Template Tags (2 posts)

  1. michaeldupuisjr
    Member
    Posted 3 years ago #

    Just getting my feet wet with Template Tags and after reading through everything the Codex has to offer, I'm still pretty confused about how to implement the Template Tags. Let's take "get_posts" for example:

    Per the get_posts page, I believe I should be able to call a post using this code:

    <?php get_posts('arguments'); ?>

    However, the example that follows looks nothing like this; here it is:

      <?php
      global $post;
      $myposts = get_posts('numberposts=5&offset=1&category=1');
      foreach($myposts as $post) :
      ?>

    • "><?php the_title(); ?>
    • <?php endforeach; ?>

    The get_posts section doesn't come until the 4th line here. My question is, what are the elements preceding the get_posts (i.e., what is "global" and "$post" and "$myposts") and where can I read up on them?

    Thanks!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    $post is a WP variable containing the post data. global is a PHP keyword that allows you access to $post variable in your template - even though the variable was assigned elsewhere in the WP core.

    http://uk2.php.net/manual/en/language.variables.scope.php

    $myposts is simply an example of a user variable in a template that is being used to store the post data and which can then be manipulated without impacting anything beyond the scope of that template file.

Topic Closed

This topic has been closed to new replies.

About this Topic