Customizing the custom loop wordcamp 2012

3,306 views

Published on

Using WP_Query to create a custom theme

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,306
On SlideShare
0
From Embeds
0
Number of Embeds
1,748
Actions
Shares
0
Downloads
20
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Customizing the custom loop wordcamp 2012

  1. 1. Welcome CUSTOMIZING THE CUSTOM LOOPCode: http://github.com/jmarx/Wordcamp-NYC-2012
  2. 2. Meet the guysAlexander Sapountzis Jeff Marx• Worked with • Worked with WordPress for 2 WordPress for 7 years Years • Worked a Gannet• Worked at Bloomberg Newspapers Businessweek • Snappy Dresser• Hes the one with the beard
  3. 3. What is CBS Local?Network of local sites for news and media, consolidating CBS media content• Radio• TV• News• Original Contentwww.cbslocal.com
  4. 4. We use WordPress!
  5. 5. After the five-minute install • • You get a cool theme • • Single Column Sidebar ...but its boring We need to customize this so its cool!
  6. 6. What comes next?• Featured Posts• Category Boxes• Cool stuff• Not boring!
  7. 7. How do we do this?(PAY ATTENTION, THIS WILL BE ON THE TEST)
  8. 8. The Global Query• Runs once on every page• Returns all relevant posts based on params o Home Page Query returns all posts o Category Page Query returns all for that category o Single Post Page Query returns a single, lonely post• Global query feeds default loop o default loop is hard coded on theme template files and contains post information from the global query• This comes built into WordPress
  9. 9. WordPress does all this for free...whats the problem? Why bother with custom loops?Because you may need to:• Categorize content• use Custom Post Types• not have a boring site
  10. 10. Introduce The Loops• query_posts()• WP_Query• get_posts()Each is used in different ways, but you really only need one; WP_Query
  11. 11. query_posts()• Modifies the Global Query (thats bad)• Can cause unpredictable behavior• Will very likely be removed from WordPress (we hope soon)• Ignore tutorials that use thisRemember: using query_posts() is like trying to hammer a nail in with your forehead; painful and messy
  12. 12. get_posts()• Safer but not ideal• Limited - Not as powerful or useful• Some template tags are not includedThis is like the little brother of WP_Query. It uses WP_Query behind the scenes, but hides quite a bit from you
  13. 13. WP_Query• Returns everything you need• Template tags will work• Plays nice with the Global Query• High in fiberquery_posts() and get_posts() actually use this internally, but do other things that you dont want. Cut out the middle man!
  14. 14. Standard LoopExamples of the standard loop (before we make it all fancy)https://github.com/jmarx/Wordcamp-NYC- 2012/blob/master/twentyten_jeff/standard- loop.php
  15. 15. 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 Page https://github.com/jmarx/Wordcamp-NYC- 2012/blob/master/twentyten_jeff/page_dc.php
  16. 16. Performance Gotchas• Use caching and transients to improve performance• Limits load by minimizing database calls• Dont need too many loops on your site
  17. 17. Got Questions?@jeffreymarx@asapountzisCode Samples:https://github.com/jmarx/Wordcamp-NYC-2012

×