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.
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.
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:
Also formal learning opps like:
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.
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.
Check ot the WP_Post Object to see everything in the post (the template tags pretty much mirror these)
First, check IF we have post…
WHILE we have post…
then we do stuff
$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)
Lots of interesting Starbucks references here. Methinks Randy has spent some time in a green apron.
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.
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.
The WordPress Loop
have_posts() iterates the loop one post at a time by returning true if there is a post to display.
People in a line – “foreach” person, make the drink they paid for.
$peopleinline = array ( ‘John’ => ‘coffee’, ‘Mary’ => ‘Americano’ );
foreach ( $peopleinline as $customer => drink )
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.
Basis of presentation is here -> https://github.com/wpedu/The-WordPress-Loop
You can find Randy online at visualcoma.com, @vs26, github.com/vc27
He’s very involved in the Nashville WordPress community.
Pre-talk: Randy’s encouraging us to find one of the WordCamp organizers and say “thank you.”