Lyza Loop WordPress Plugin: Documentation, Usage, Examples

Quick Start

The most basic Lyza Loop usage looks like this:

<?php lyza_loop(); ?>

Learn More

This will:

  • Use your default loop template (you can set this in the Lyza Loop settings admin screen–on installation this is set to ’simple_UL’)
  • This will respect the value set for your blog in Settings -> Reading -> “Blog pages show at most ” for determining how many posts to retrieve (overridden with the ’showposts’ argument).
  • Will simply get the most recent posts.
  • Will pick up your Lyza Loop default setting for suppressing repeat posts (or not).

Of course, you probably want to do something a bit more clever than that, so you might do:

<?php
    $args = Array('template'            => 'really_cool_stuff',
                  'showposts'           => 4,
                  'orderby'             => 'title',
                  'category__not_in'    => 35,
                  'exclude_repeats'     => false);
?>

In this hypothetical situation:

  • You have created a custom loop template called ‘really_cool_stuff.php’ and you are using it here.
  • You only want to show four posts/pages, you want to sort by title and exclude a specific category because you feel like it. These arguments are all query_posts() parameters.
  • You don’t care if this custom loop repeats posts that have already been displayed on the page.

Creating Loop Templates

A loop template is a PHP file with HTML markup, WordPress template tags, and Lyza Loop batching variables that is used to render posts.

Lyza Loop comes with a few default loop templates, but they’re not that great. You’ll probably want to create your own!

Custom loop templates go in the location indicated in the Lyza Loop settings admin panel. By default this is [your theme's directory]/loop_templates. Lyza Loop will automatically see any PHP file in this location and recognize it as a loop template.

Some notes about loop templates:

  • You have access to all WordPress template tags (e.g. the_title() ).
  • The template gets rendered once for each item in the list. Need it to behave differently for some of the items in your loop? Take a look at Batching Support section.

Batching Support

Lyza Loop provides some convenience batching information that is available in loop templates. The available batching variables are:

    $loop_count     // Our current item number in the loop
    $loop_odd       // True for odd items in the loop (item 1, 3, 5, etc.)
    $loop_even      // True for even items in the loop (item 2, 4, 6, etc.)
    $loop_first     // True when first item in loop
    $loop_last      // True when last item in loop
    $loop_css_class // Either 'even' or 'odd'
    $loop_size      // Total number of items in loop
    $loop_owner     // WordPress Post object corresponding to the
                    // global post context when lyza_loop() was called

lyza_loop() Arguments

lyza_loop() takes arguments in standard WordPress argument syntax.

Lyza Loop-specific Parameters

    'template':         string name of loop template to use, without extension (.php)
                        example: 'my_template'
    'exclude_repeats':  Boolean overrides default setting for repeat suppression
                        example: false (allow duplicates)
    'use_template':     Boolean render using a template?
                        example: false (don't render at all, just return matching post objects)

Other Parameters

lyza_loop() takes any WordPress query_posts() parameter. Please see documentation on query_posts() for more information.

A Few More Specifics

  • Lyza Loop always respects the context from which it was called. Global $post and $wp_query objects are put back as they were.
  • lyza_loop() returns an Array of Post objects — the posts that were returned by query_posts(), should you need them
  • Setting ‘use_template’ to false will suppress rendering and will return the post objects only. This is probably not too useful, but does provide a method of keeping track of seen posts if repeat suppression is needed later, and makes sure not to disturb the $wp_query and global $post contexts.
Wonderful games with Caslon