$30 off During Our Annual Pro Sale. View Details »

Be The Loop: WordCamp Chicago 2017

Be The Loop: WordCamp Chicago 2017

We live inside the boundaries of The Loop. As an intrinsic part of WordPress, does it mystify you? Are you feeling a bit stuck in there? Want to expand its limits? In this talk, we’ll go over what the loop is all about as well as learn more about the creative possibilities of customizing what data you’re retrieving through the use of WP_Query to write your very own queries and loops.

Allison Tarr

April 29, 2017
Tweet

More Decks by Allison Tarr

Other Decks in Technology

Transcript

  1. Be The Loop
    You Want To
    See In The
    World

    View Slide

  2. H’allo there…
    I’m Allison.
    ◎ Feminist. Writer. Lover
    of guacamole.
    ◎ Front-end developer at
    WebDevStudios
    ◎ @allisonplus

    View Slide

  3. We’re going to go over...
    ◎ What is The Loop?
    ◎ The power of customizing WP_Query();
    ◎ Examples of customizations

    View Slide

  4. 1.
    AHHHHHHHHH!
    The Loop.
    Let’s start with taking a deep breath.

    View Slide


  5. The loop is PHP code used by
    WordPress to display posts.
    https://codex.wordpress.org/The_Loop

    View Slide

  6. What does it do?
    ◎ It’s the thing that goes and gets your
    content from the database.
    ◎ Loops over posts to bring in data.
    ◎ Makes our lives easier!

    View Slide

  7. View Slide

  8. The Loop is Lovely
    because it...
    ◎ Saves time
    ◎ Keeps your code condensed & tidy
    ◎ Efficient!

    View Slide

  9. View Slide

  10. The Loop in real talk.
    If there are any posts
    And only while there are still posts,
    Give me the post!
    (if not, do something else)

    View Slide

  11. View Slide

  12. View Slide

  13. What can the Loop output?
    ◎ Template Tags
    ◎ HTML your content is wrapped in
    ◎ Customizations (if any)

    View Slide

  14. View Slide

  15. Every Loop has a
    query.
    The query is the request for the
    information your Loop will have
    access to.

    View Slide

  16. In the regular ol’ Loop...
    WordPress automatically
    loads a global variable to
    get the appropriate
    content.

    View Slide

  17. What if I want more?
    What if
    that loop
    isn’t good
    enough?

    View Slide

  18. WP_Query()
    Blah blah blah.
    https://codex.wordpress.org/Class_Reference/WP_Query

    View Slide

  19. There’s a few
    different ways to
    go about this
    ● WP_Query()
    ● pre_get_posts()
    ● query_posts()

    View Slide

  20. Custom Post Types
    For use within plugins &/or widgets
    Posts by multiple authors
    Why would we need a
    custom query?

    View Slide

  21. WP_Query Ingredients
    ◎ Define what you’re asking for
    ◎ Arguments for the query
    ◎ The Loop
    ◎ Leave no trace.

    View Slide

  22. View Slide

  23. Arguments
    Define it.
    Loop
    Clean-up!

    View Slide

  24. Arguments
    Your arguments feed the query.
    The query feeds The Loop.
    https://codex.wordpress.org/Class_Reference/WP_Query#Parameters

    View Slide

  25. View Slide

  26. View Slide

  27. Define Your Query
    $fancy_new_query = new WP_Query( $args );

    View Slide

  28. The Loop

    View Slide

  29. Leave No Trace.

    View Slide

  30. wp_reset_postdata();
    Resets the global $post data we’ve
    been messing with after a custom
    query.
    https://codex.wordpress.org/wp_reset_postdata

    View Slide

  31. View Slide

  32. Display a sticky post
    I can do anything?!
    Grabbing a random
    selection from a
    Custom Post Type

    View Slide

  33. Random Team Member

    View Slide

  34. Display a Sticky Post

    View Slide

  35. Thanks!
    Any questions?
    Come hang out with me online at:
    ◎ @allisonplus
    [email protected]
    ◎ allisontarr.com
    Presentation template by SlidesCarnival

    View Slide