The WordPress Loop Demystified

Audience: Intermediate
Presented by: Randy Hicks

Description: The WordPress loop is only magical until you grasp the moving parts. In this presentation we’ll uncover the gears that keep the loop running and how to access it mystical powers.

Presentation slides are here.

Below are notes from the presentation, live blog style.

Carrie Dils March 1, 201412:41 pm

That’s it folks. Randy did a great job. He promotes community and learning. Follow him on the twitters at <a href="https://twitter.com/vc27"@vc27.

Carrie Dils March 1, 201412:30 pm

Q: For a beginner, would you recommend learning PHP first or starting with the loop?

A: First step is to get an installation of WordPress somewhere. Open up the TwentyFourteen theme and orient yourself with the file names and template hierarchy (https://codex.wordpress.org/Template_Hierarchy). Template hierarchy maps out how WordPress “knows” which page to grab to display information. Learn what the files are doing.

Also good learning resources:

http://www.wpbeginner.com/

https://tutsplus.com/

Also formal learning opps like:

http://teamtreehouse.com/

Carrie Dils March 1, 201412:22 pm

Q&A

Carrie Dils March 1, 201412:20 pm

Resources:

http://codex.wordpress.org/The_Loop

https://codex.wordpress.org/The_Loop_in_Action

http://phpxref.ftwr.co.uk/wordpress/nav.html?index.html (use the search)

Don’t be afraid to open up the code and get your hands dirty. You will start to absorb this stuff.

Carrie Dils March 1, 201412:17 pm

Don’t be shy about learning!

If you’re getting into code and want to start manipulating just small parts of something, just go for it. Go in and fiddle with stuff and see how it changes things. It’ll help you learn and get familiar with the code.

PRO tip -> Don’t try stuff on your live site. Duplicate it somewhere and use a development environment.

Carrie Dils March 1, 201412:14 pm

Check ot the WP_Post Object to see everything in the post (the template tags pretty much mirror these)

Carrie Dils March 1, 201412:11 pm

The Loop:

First, check IF we have post…
WHILE we have post…
then we do stuff

Carrie Dils March 1, 201412:09 pm

Globals (https://codex.wordpress.org/Global_Variables)

$post which is set by the_post();

Don’t use globals until you understand more about variable scope. (http://www.php.net/manual/en/language.variables.scope.php)

Carrie Dils March 1, 201412:07 pm

Lots of interesting Starbucks references here. Methinks Randy has spent some time in a green apron.

Carrie Dils March 1, 201412:07 pm

So how do you access loop variables?

Things like the_title(), the_content(), etc. WordPress has template tags – a standard php function designed to be used in a special location with a special purpose. In this case, they only work within the loop.

https://codex.wordpress.org/Template_Tags

They only work WITHIN the loop. You can’t access loop variables outside the loop. So things like the_title() have no meaning or context outside of the loop.

Carrie Dils March 1, 201412:05 pm

The WordPress Loop

In short, have_posts() iterates the loop one post at a time by returning true if there is a post to display.

Carrie Dils March 1, 201412:03 pm

People in a line – “foreach” person, make the drink they paid for.

$peopleinline = array ( ‘John’ => ‘coffee’, ‘Mary’ => ‘Americano’ );

//foreach loop

foreach ( $peopleinline as $customer => drink )

Carrie Dils March 1, 201412:02 pm

What is a loop?

A loop is a repeating process which runs a blog of code for each item in an array. (Example: Making drinks for each person in line at Starbucks. The poor sap at the register is the process/code inside the loop). It can go on indefinitely.

Carrie Dils March 1, 201411:59 am

Basis of presentation is here -> https://github.com/wpedu/The-WordPress-Loop

Carrie Dils March 1, 201411:56 am

You can find Randy online at visualcoma.com, @vs26, github.com/vc27

He’s very involved in the Nashville WordPress community.

Carrie Dils March 1, 201411:56 am

Pre-talk: Randy’s encouraging us to find one of the WordCamp organizers and say “thank you.” :)

Comments

Leave a Reply