WordPress main loop and global variables

Source: Internet
Author: User
Tags php foreach php template

For specific WordPress actions and Filters, you can easily know where they are executed in the main loop. However, sometimes you do not want to use the action or filter in the main loop and just want to invoke the template function (templates tag). At this point, you need to be very familiar with the global variables you want to access and the possible results you get.

Below I will explain the main WordPress loop so that you can better understand which global variables can be called by the template function in the main loop.

WordPress main loop->the WordPress loop

WordPress main loop is used to display a list of logs on some pages and to display a list of messages on a single log page page.

In index.php of the default theme, the main loop begins with the following code:

<?php if (have_posts ()):? ><?php while (Have_posts ()): The_post ();?>


Both functions have_posts () and The_post () are property functions that can access methods in class Wp_query

Have_posts This function to see whether the currently displayed number of logs has reached the number of logs to be displayed in the background of WordPress, the main loop will continue until it is reached.

If you have studied the The_post function, you will find that it searches $post this global variable. The The_post function gets the first log (the subsequent log is obtained by iterating through each iteration) and sets the log data (such as author, multiplicity-page, etc.), which is the WordPress global assignment $post This variable, so you can access it.

After the The_post () function is called, you can use many template functions and global variables.

Here are some examples of the template functions available now:

The_author: Gets the name of the author.

The_title: Displays the title of the current log.

The_content: Displays the contents of the log.

The_permalink: Displays the URL of the permanent link for the log.

THE_ID: Displays the ID of the log.

The following are the global variables available now:

Global variable AuthorData, you can call it using the following code:

Global $authordata, Echo $authordata->display_name;


By AuthorData This global variable also allows you to get: Last_name,first_name,id,user_email,user_url,user_login,description and others.

Global variable post, you can call it using the following code:

This global variable via post also allows you to get: Id,post_author,post_date,post_excerpt,comment_count and others.

Global variable post, you can call it by using the following code in a function:

Global $post, Echo $post->post_content;


The log content obtained by this method is not filtered. If you want to manipulate the contents of the log according to your liking, such as setting the display of the log content yourself, the log content obtained in this way is very useful.

Although there is no fast global access, the The_permalink function is the result of the output function Get_permalink in $id the variable has been set.

Global variable ID, you can call it using the following code:

Global $id; echo $id;


An example of a template function

Suppose you write a template function called Get_my_trackback, which responds to trackback every time a message loop is detected. We will put this function into the foreach message loop of the comment.php template file.

<?php foreach ($comments as $comment):? ><?php get_my_trackback ();? ><?php Endforeach; /* End for each comment */?>


The Get_my_trackback function code is as follows:

<?php function Get_my_trackback () {Global $comment;    if (empty ($comment)) return; if ($comment->comment_type! = ' comment ') {//do trackback stuff}}?>


Comments This global variable gives you access to the details of your current message, so you can do anything about it.

Conclusion

In this article, the WordPress main loop and global variables are analyzed in detail, so you can use them very flexibly when making plugins or modifying themes later.

WordPress main loop and global variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.