getting into the loop

Post on 29-Jan-2018

45.176 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Getting Into The Loopmitcho (Michael 芳貴 Erlewine)http://mitcho.com

January 23, 2010WordCamp Boston

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

NOTE:• Will link to slides and code later

on http://mitcho.com/blog/• Please rate later on SpeakerRate

(find link on the blog)

Today

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Introduction

Introduction• Hi, I’m mitcho.

Introduction• Hi, I’m mitcho.

• Linguist, coder, teacher.

Introduction• Hi, I’m mitcho.

• Linguist, coder, teacher.• At MIT. Previously at Mozilla. Now

working with Automattic.

Introduction• Hi, I’m mitcho.

• Linguist, coder, teacher.• At MIT. Previously at Mozilla. Now

working with Automattic.• Programming PHP/MySQL since

2002?

Introduction• Hi, I’m mitcho.

• Linguist, coder, teacher.• At MIT. Previously at Mozilla. Now

working with Automattic.• Programming PHP/MySQL since

2002?• Blogging (off and on) since 2007.

Introduction• Hi, I’m mitcho.

• Linguist, coder, teacher.• At MIT. Previously at Mozilla. Now

working with Automattic.• Programming PHP/MySQL since

2002?• Blogging (off and on) since 2007.• http://mitcho.com;

@mitchoyoshitaka

Introduction

• Yet Another Related Posts Plugin

Introduction

• Yet Another Related Posts Plugin• ...aka YARPP

Introduction

• Yet Another Related Posts Plugin• ...aka YARPP• As seen on Laughing Squid, ma.tt...

Introduction

• Yet Another Related Posts Plugin• ...aka YARPP• As seen on Laughing Squid, ma.tt...• Over 350k downloads

Introduction

• Yet Another Related Posts Plugin• ...aka YARPP• As seen on Laughing Squid, ma.tt...• Over 350k downloads

• http://mitcho.com/code/yarpp or search for “YARPP”; @yarpp

Introduction

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

The Loop

The Loop

• “The Loop” is the mechanism by which posts are called from the database and then displayed.

The Loop

• “The Loop” is the mechanism by which posts are called from the database and then displayed.

• On many pages—like the index or archives—it “loops” through each post.

The simplest Loopget_header();if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile;endif;get_sidebar();get_footer();

The simplest Loopget_header();if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile;endif;get_sidebar();get_footer();

{“The Loop”

The simplest Loopget_header();if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile;endif;get_sidebar();get_footer();

{“The Loop” Sets up the post

The simplest Loop

Every theme’s PHP files are built on this basic structure.

get_header();if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile;endif;get_sidebar();get_footer();

{“The Loop” Sets up the post

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

{“The Loop”

The Loop

The Loop

• The Loop is where you can use Template Tags.

The Loop

• The Loop is where you can use Template Tags.• codex.wordpress.org/

Template_Tags

The Loop

• The Loop is where you can use Template Tags.• codex.wordpress.org/

Template_Tags • It’s the the_post() call that makes that

possible.

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

CC BY flickr.com/photos/myklroventine/1430113497/

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

But mommy, where doposts come from?

CC BY flickr.com/photos/myklroventine/1430113497/

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

But mommy, where doposts come from?

CC BY flickr.com/photos/myklroventine/1430113497/

Every Loop has a query

Every Loop has a query

• Regularly, WordPress chooses the right template file and query based on your URL.

Every Loop has a query

• Regularly, WordPress chooses the right template file and query based on your URL.• codex.wordpress.org/

Template_Hierarchy

Every Loop has a query

• Regularly, WordPress chooses the right template file and query based on your URL.• codex.wordpress.org/

Template_Hierarchy• /archives/123 → single post

Every Loop has a query

• Regularly, WordPress chooses the right template file and query based on your URL.• codex.wordpress.org/

Template_Hierarchy• /archives/123 → single post• /archives→ archives

Every Loop has a query

• Regularly, WordPress chooses the right template file and query based on your URL.• codex.wordpress.org/

Template_Hierarchy• /archives/123 → single post• /archives→ archives• /tags/chicken→ all chicken articles

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Customizing The Loop

Customizing The Loop

• Themes control how information is presented...

Customizing The Loop

• Themes control how information is presented...

• The query controls what information is presented.

Custom queries

Custom queries

Possible applications:

Custom queries

Possible applications:• Create custom feeds/displays

Custom queries

Possible applications:• Create custom feeds/displays

• ephramzerb.com/projects/feed-wrangler/

Custom queries

Possible applications:• Create custom feeds/displays

• ephramzerb.com/projects/feed-wrangler/

• Pull information on other posts from within the theme’s Loop

Custom queries

Possible applications:• Create custom feeds/displays

• ephramzerb.com/projects/feed-wrangler/

• Pull information on other posts from within the theme’s Loop

• Customize what information is displayed globally

Custom queries

Possible applications:• Create custom feeds/displays

• ephramzerb.com/projects/feed-wrangler/

• Pull information on other posts from within the theme’s Loop

• Customize what information is displayed globally

Today’sexamples

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Roll your own query

Roll your own query

EX:

Roll your own query

EX:• Pull information on other posts

from within the theme’s Loop

Roll your own query

EX:• Pull information on other posts

from within the theme’s Loop• Display other posts with a specific

criteria, like a tag.

Roll your own query

EX:• Pull information on other posts

from within the theme’s Loop• Display other posts with a specific

criteria, like a tag.• Wrap it up in a shortcode [others]

Roll your own query

Roll your own query

The idea:

Roll your own query

The idea:• Create a new WP_Query object.

Roll your own query

The idea:• Create a new WP_Query object.

• Given a $tag...

Roll your own query

The idea:• Create a new WP_Query object.

• Given a $tag...• new WP_Query(array('tag'=>

$tag))

Roll your own query

The idea:• Create a new WP_Query object.

• Given a $tag...• new WP_Query(array('tag'=>

$tag))• Call it $my_query

Roll your own query

The idea:• Create a new WP_Query object.

• Given a $tag...• new WP_Query(array('tag'=>

$tag))• Call it $my_query

• Create a Loop for $my_query

Roll your own query

The idea:• Create a new WP_Query object.

• Given a $tag...• new WP_Query(array('tag'=>

$tag))• Call it $my_query

• Create a Loop for $my_query• Do stuff in it

Make your own Loop

Make sure the Loop controllers are using $my_query, not the default ($wp_query)

if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); the_content(); endwhile;endif;

The result:

DEMO:

More about WP_Query

More about WP_Query

Learn more from the Codex:

More about WP_Query

Learn more from the Codex:• More information on the

parameters:

More about WP_Query

Learn more from the Codex:• More information on the

parameters:• codex.wordpress.org/

Template_Tags/query_posts

More about WP_Query

Learn more from the Codex:• More information on the

parameters:• codex.wordpress.org/

Template_Tags/query_posts• Tips and examples:

More about WP_Query

Learn more from the Codex:• More information on the

parameters:• codex.wordpress.org/

Template_Tags/query_posts• Tips and examples:

• codex.wordpress.org/The_Loop

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Filter every query

Filter every query

EX:

Filter every query

EX:• Customize what information

is displayed globally.

Filter every query

EX:• Customize what information

is displayed globally.• Hide all my tweets.

Filter every query

The idea:

Filter every query

The idea:• Use the request filter to specify

that we don’t want results from the “tweet” category (#10)

Filter every query

The result:

DEMO

Aside: the tools

Turn up the fire!

• Firefox getfirefox.com

• Firebug getfirebug.com

• FirePHP firephp.org

Aside: the tools

Turn up the fire!

• Firefox getfirefox.com

• Firebug getfirebug.com

• FirePHP firephp.org

The “Pyrotrinity”

FirePHP

Takes a moment to set up:

FirePHP

Takes a moment to set up:

Use it like this:

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Next steps

Hit the docs:

Next steps

Hit the docs:• PHP manual: php.net

Next steps

Hit the docs:• PHP manual: php.net• Codex: codex.wordpress.org

Next steps

Hit the docs:• PHP manual: php.net• Codex: codex.wordpress.org• The source:

core.trac.wordpress.org

Next steps

Hit the docs:• PHP manual: php.net• Codex: codex.wordpress.org• The source:

core.trac.wordpress.org• grep it!

Next steps

Next steps

Learn SQL

Next steps

Learn SQL• The language that WordPress’s raw

database calls are in.

Next steps

Learn SQL• The language that WordPress’s raw

database calls are in.• It’s really not that scary.

Next steps

Learn SQL• The language that WordPress’s raw

database calls are in.• It’s really not that scary.• Lets you write filters directly on

different parts of the query.

Next steps

Learn SQL• The language that WordPress’s raw

database calls are in.• It’s really not that scary.• Lets you write filters directly on

different parts of the query.• EX: mitcho.com/blog/how-to/external-

orders-in-wordpress-queries/

Next steps

Thank you!Questions?

Slides will be up on mitcho.com/blog/.See you at the Genius Bar!

mitcho (Michael 芳貴 Erlewine)mitcho.com; @mitchoyoshitaka

top related