17
Deconstructing the Loop AL DAVIS WPTEACH.COM [email protected]

Deconstructing the Loop AL DAVIS WPTEACH.COM [email protected]

Embed Size (px)

Citation preview

Page 1: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

Deconstructing the Loop

AL DAVIS WPTEACH.COM [email protected]

Page 2: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

ABOUTME

Organizer of:• WordCamp Toronto ( 2011, 2012)• WordCamp Developers Toronto ( 2012)• Toronto WordPress Meetup group• Toronto Wordpress Developers Meetup

Who is that bald guy at the front of the room? And why is he there?

AL DAVIS WPTEACH.COM [email protected]

Program Manager, Hostopia• WordPress SME for world’s largest

wholesale hosting provider• Developing WP strategy for major telco’s,

cablecos and ISP’s globally• About to launch a service where we design

and publish 1000+ sites a month using WPProfessor , George Brown College

• Intro to WordPress• Intro to WordPress theme Development Owner, WPTeach.com

• WordPress website• Launching online school to learn WP in Q2• Freelance WordPress Development

Page 3: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

LETS GET TO IT THEN

AL DAVIS WPTEACH.COM [email protected]

From the codex:

     "The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post."

What is the Loop?

My definition:

     The Loop is a living, breathing thing. it's the engine that makes WordPress as powerful as it is, it makes everything tick. Without the loop, there is no dynamic content.

Using the loop. we can display any content we want, however we want.

Page 4: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

What does it look like?

The Loop I mean….

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

Page 5: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

Topic

AL DAVIS WPTEACH.COM [email protected]

Thanks for your time…..

Just kidding…..

Page 6: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

<?php if (have_posts()): while(have_posts()): the_post();?>

IN THE BEGINNING…

Part 1

<?php  ?> - opens and closes the PHP command

Part 2

have_posts() - checks to see there are posts to display

Part3

the_post- unpacks the next post in the queue.

In Plain English

Using PHP, check to see if there are any posts to display, if there are , then display them. Stop using PHP.

Page 7: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

THE ENDING…

Part 1

<?php endwhile; else: ?> STOP the Loop....but notice the "else" thats in there.....

The very first "if" if (have_posts()) tested to see if there were any Posts to display. This "else" part tells what do if there weren't any

Page 8: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

THE ENDING…

Part 2

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

Nothing to look at here...move along

Page 9: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

THE ENDING…

Part 3

<?php endif; ?> Seriously, stop the loop....now.

Page 10: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

//Magic happens here

<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

ALL TOGETHER NOW…..

In Plain English

Using PHP, check to see if there are any posts to display, if there are , then display them. If you have reached the number of posts I told you to get, then stop. If you don’t find any post based on what I asked for, display the message I told you to.Now seriously, stop…no more.

Page 11: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

So how does it look?

Blank, there is nothing there….

But the good news is, if you have a blank screen you have done nothing wrong..

So how do we make it work like we think it should?

Page 12: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

Say hello to your new BFF The Template Tag

What is a Template Tag?

A PHP function used in your WordPress theme template to display specific pieces of data about your web site and your content.

This allows you to customize how and where your content is displayed on your website.

Example

the_title() displays the title of your post or page inside the Loop

Page 13: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

Why Use Template Tag?

Saves you from learning PHP, which makes it worth the price of admission right there

Page 14: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

Commonly used Template Tags

<?php the_permalink();?>- returns the URL of your post

<?php the_title();?> - returns the title of your post or page

<?php the_ID();?> -returns the unique ID of your post

<?php the_excerpt();?> -just the excerpt of the post

<?php the_time();?> displays the date/time the post was published

<?php the_author();?>- displays the author of the post

<?php the_category();?>- displays the categories assigned to the post

<?php the_tags();?> - Displays the tags assigned to the post

<?php comments_popup_link();?> Link for the comments on the post

Page 15: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

DECONSTRUCTINGTHELOOP

AL DAVIS WPTEACH.COM [email protected]

Lets see what it looks like in action, shall we?

Page 16: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

AL DAVIS WPTEACH.COM [email protected]

?Questions

Page 17: Deconstructing the Loop AL DAVIS WPTEACH.COM AL@WPTEACH.COM

AL DAVIS WPTEACH.COM [email protected]

Thank You

Slides are up at wpteach.com