Accomplishment this Week
Reading: Professional WordPress, Chapter 5 Loop
After finishing the chapter about core files, I started learning about Loop in WordPress. Loop controls the content fetched from database and displayed on the page that is loading. If I want to display some content different from the default ones, for example, if I want to display posts alphabetically instead of chronologically, I can use custom Loop to realize that.
The work flow of a default Loop is as follows:
- If the URL matches some file on web server, the file will be directly loaded without WordPress getting involved.
- Else, when the URL of the page is passed to WordPress, the parse_query() method of WP_Query Object will parse the URL into a sequence of query parameters.
- Then get_posts() method of WP_Query Object will convert these query parameters into SQL query and run the query to fetch the post data from MySQL database. The raw post data fetched will be stored in a global variable $wp_query of the WP_Query Object.
- After getting the data, WordPress set all conditional tags like is_home and is_single.
- Then WordPress chooses a template file within the theme based on the Template Hierarchy.
- The code inside template files will display the posts. The skeleton is shown in the picture below:
- In every iteration, the_post() method in turn calls setup_postdata() method which sets up the global variable $post. The post-related information of this particular post stored in this variable is not raw data, but filtered content (related to plugin). After the Loop, $post variable stores the data of the last post displayed.
- The loop content will be displayed using template tags, which are PHP functions used to display post-related content. For example, the_title(), the_content() and the_excerpt(). Many template tags have parameters like the_title(’<h1>’, ‘</h1>’) which takes two parameters $before and $after.
Customization of Loop
If I want to customize the Loop, I can build my new WP_Query Object or use lower-level functions query_posts() and get_posts(). Among these, creating a new WP_Query Object is the referred way.
- New WP_Query Object
Creating a new WP_Query Object and making query with parameters/arguments will return the posts desired. (key here is to choose appropriate arguments)
This is similar to the default query. The difference is that the arguments are not from URL but given explicitly, and the methods have_posts() and the_post() are called from newly created WP_Query Object instead of the global variable $wp_query.
<?php $myPosts = new WP_Query(); $myPosts->query(’posts_ per_ page=5’); if($myPosts->have_ posts()): while ($myPosts->have_ posts()) : $myPosts->the_ post(); ?> <!-- do something --> <?php endwhile; end if;?>
$myPosts->query() calls get_posts() method of WP_Query Object to get posts and set flags like is_home and is_single. Template file within the theme will be chosen to display the content based
on Template Hierarchy. (same to default Loop)
query_posts($arg) function should be placed right before the start of Loop, which is if(have_posts()). It explicitly overwrite the content returned from the default Loop. It should not be used more than once and should not be used inside the Loop. It may result in wrong conditional tags because tags are set during the default Loop which has different return content from the custom Loop. It also slows down the loading because it executes another query. wp_reset_query() function should be added manually after the Loop to restore the original query.
get_posts($arg) function returns an array of raw post data and needs to manually add setup_postdata() function in order to use template tags.
Visit the site: For anyone who is outside the NTU intranet, he/she can use NTU VPN first and go to the address http://172.19.37.161/wordpress (172.19.37.161 is the IP address of our server) to visit the WordPress home page. Cynthia and I can login at http://172.19.37.161/wordpress/wp-login.php to access the dashboard.
Now we have setup one WordPress on this server. If we want to have two WordPress we need to assign them to different port numbers. We will do that next week.
Planning for the next week
Next week we will finish the 2-week project. We planned to start coding on this Wednesday but were postponed. We will start doing that after Beverley give us her design and we will push the result onto the live server when finished.
I will continue reading the book Professional WordPress as scheduled.