Build Your Own Business Website header image

Lesson 14 – Using Secondary WordPress Loops

Difficulty Level -

Filed Under Topics - , ,

Listed Under Lesson Subjects -

Applies to -

In this Thesis theme video tutorial lesson of our Customize Thesis Like a Pro course you will learn how to use secondary WordPress Loops. We begin by explaining the WordPress Loop system, discuss both the main and secondary loops and show examples of these. Then we examine the anatomy of secondary WordPress loops including ways to create them, what they are constructed of, and the parameters.

We follow up the basic instruction with examples of how to use secondary loops focusing on displaying Thesis teasers, Thesis post images and excerpts. We wrap up the lesson by demonstrating how to use shortcodes to add secondary loops to pages and post.

What is a WordPress Loop?

  • The process of getting information from the database and displaying it on the page
  • Loops in action
    • The page loop – simple loop, retrieves one piece of information
    • The single post loop – the same
    • Blog page loop – retrieves several posts and loops through them
    • Archive page loop
  • The main loop
    • Retrieves the primary content of the page
    • Can contain secondary loops
  • Secondary loops
    • Any other loop besides the main loop
    • Examples of secondary loops
      • comments
      • widgets
      • byobwebsite examples

Anatomy of a Secondary WordPress Loop

  • Several ways to create a secondary loop
    • query posts – this is the main query object
    • WP Query – the preferred method
      • Creates a new query
      • Separate from the main query
  • 4 parts of a Secondary Loop
    • Define the query parameters
    • The query
    • Display the results
    • Clean up
  • WordPress Query Parameters
    • Category parameters
    • Tag parameters
    • Post & page parameters
    • Pagination parameters

Example #1 – Add a List of Post Teasers From a Single Category

  • Create the function
  • Define the Query Parameters
    • category
    • posts per page
  • Write the query
  • Configure the output
    • Thesis theme teaser
      • 3 parameters that we need to define but won’t use
  • Clean up

Example #2 – Create a Shortcode for the List

  • What’s a shortcode?
    • A way of executing a function from within the content
  • The shortcode needs to “return” some content
    • Create the content variable & add content to it
    • Return the content variable
  • add_shortcode syntax
  • Shortcode output problems
    • Some WP functions are designed to immediately output their information
    • Solution – stop the output and place all ouput in a buffer
  • Wrap the output in a buffer – 3 parts
    • Start the output buffer – ob_start()
    • Place the output buffer in the variable – ob_get_contents()
    • Clean the output buffer – ob_end_clean()

Example #3 – Create a grid of Thesis Post Thumbnails

  • thesis_post_image_info function
  • Display the ‘output’ element of the array
  • Link the image to the post
    • get_permalink();
    • get_the_title();

Example #4 – Make the Loop Shortcode Configurable

  • The $atts variable
  • extract shortcode_atts syntax
    • extracts the attributes
    • initializes any without values
    • places the values in named variables
  • Use the named variables within the query
  • Add the Shortcode to the page
  • Make shortcodes work in text widgets

Question and Answer Session

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
Subscription Options
0 Comments… add one
0 comments… add one