WordPress Performance Optimization - Jeff Matson - WordCamp Milwaukee 2014
Customizing the custom loop wordcamp 2012-jeff
-
Upload
jeff-marx -
Category
Technology
-
view
327 -
download
0
Transcript of Customizing the custom loop wordcamp 2012-jeff
![Page 1: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/1.jpg)
Customizing the Custom Loop
Welcome
Code: http://github.com/jmarx/Wordcamp-NYC-2012
![Page 2: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/2.jpg)
Meet the guys
Alexander Sapountzis
• Worked with WordPress for 2 Years
• Worked at Bloomberg Businessweek
• He's the one with the beard
Jeff Marx
• Worked with WordPress for 7 years
• Worked at Gannett Newspapers
• Not the guy from the Sopranos (even though he looks like him)
![Page 3: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/3.jpg)
What is CBS Local?
Network of local sites for news and media, consolidating CBS' media content
• Radio
• TV
• News
• Original Content
www.cbslocal.com
![Page 4: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/4.jpg)
We use WordPress!
![Page 5: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/5.jpg)
After the five-minute install
• You get a cool theme
• Single Column
• Sidebar
• ...but it's boring
We need to customize this so it's cool!
![Page 6: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/6.jpg)
What comes next?
• Featured Posts
• Category Boxes
• Cool stuff
• Not boring!
![Page 7: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/7.jpg)
(pay attention, this will be on the test)
How do we do this?
![Page 8: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/8.jpg)
The Global Query
• Runs once on every page
• Returns all relevant posts based on paramso Home Page Query returns all postso Category Page Query returns all for that categoryo Single Post Page Query returns a single, lonely post
• Global query feeds default loopo default loop is hard coded on theme template files and
contains post information from the global query
• This comes built into WordPress
![Page 9: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/9.jpg)
WordPress does all this for free
...what's the problem? Why bother with custom loops?
Because you may need to:
• Categorize content
• use Custom Post Types
• not have a boring site
![Page 10: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/10.jpg)
Introduce The Loops
• query_posts()
• WP_Query
• get_posts()
Each is used in different ways, but you really only need one: WP_Query
![Page 11: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/11.jpg)
query_posts()
• Modifies the Global Query (that's bad)
• Can cause unpredictable behavior
• Will very likely be removed from WordPress (we hope soon)
• Ignore tutorials that use this
Remember: using query_posts() is like trying to hammer a nail in with your forehead; painful and messy
![Page 12: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/12.jpg)
get_posts()
• Safer but not ideal
• Limited - Not as powerful or useful
• Some template tags are not included
This is like the little brother of WP_Query. It uses WP_Query behind the scenes, but hides quite a bit from you
![Page 13: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/13.jpg)
WP_Query
• Returns everything you need
• Template tags will work
• Plays nice with the Global Query
• High in fiber http://codex.wordpress.org/Class_Reference/WP_Query
query_posts() and get_posts() actually use this internally, but either do other things that you don't want or won’t give you what you want.
Cut out the middle man!
![Page 14: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/14.jpg)
Standard Loop
Examples of the standard loop (before we make it all fancy)
https://github.com/jmarx/Wordcamp-NYC-2012/blob/master/twentyten_jeff/standard-loop.php
![Page 15: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/15.jpg)
Custom Loop Examples
Homepage:https://github.com/jmarx/Wordcamp-NYC-2012/blob/master/twentyten_jeff/loop-index.php
Sidebar:https://github.com/jmarx/Wordcamp-NYC-2012/blob/master/twentyten_jeff/sidebar.php
Pagehttps://github.com/jmarx/Wordcamp-NYC-2012/blob/master/twentyten_jeff/page_dc.php
![Page 16: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/16.jpg)
Performance Gotchas
• Use caching and transients to improve performance
• Limits load by minimizing database calls
• Always cache your site when using custom loops!
Links:http://wordpress.org/extend/plugins/wp-super-cache/http://wordpress.org/extend/plugins/w3-total-cache/http://codex.wordpress.org/Transients_API
![Page 17: Customizing the custom loop wordcamp 2012-jeff](https://reader033.fdocuments.net/reader033/viewer/2022060200/559849481a28ab52168b47ed/html5/thumbnails/17.jpg)
Got Questions?
@jeffreymarx
@asapountzis
Code Samples/Resources:
https://github.com/jmarx/Wordcamp-NYC-2012