A new version of Loops & Logic just dropped and it’s packed with little features to make your next template a bit easier to write and a bit more powerful. Let’s get right into what’s new!

Filter your loops based on multiple fields

The ability to filter and sort queries is a useful way to fine-tune the output of a loop. In 2.4.1, we’ve added the ability to filter any type of loop by multiple fields using the attributes field_2, field_value_2, etc.

Replace multiple values in a single <Format> tag

You asked, we delivered. The <Format> tag now allows you to replace multiple strings at once within a single tag using the attributes replace_2, with_2, etc. For example, the following markup would output the words Test Entry.

  <Format replace="Good" with="Test" replace_2="Morning" with_2="Entry">Good Morning</Format>

New URL Query formating

For those wanting to dynamically reformat query strings, the new url_query format type allows you to encode a URL query string from standard text. For example, the markup below would output a URL that reformates the phrase “Oh boy, a query parameter!” into the query parameter Oh%20boy%2C%20a%20query%20parameter%21.

  <Set my_query><Format url_query>Oh boy, a query parameter!</Format></Set>
  <a href="https://loopsandlogic.com/?query={Get my_query}">Click here</a>

Compare against multiple values in a single <If> tag (plus some handy shortcuts)

The <If> tag now makes it possible to compare the subject of a condition with multiple values. Simply add another comparison attribute to quickly create more complex conditional statements.

  <If field=weight more_than=5 less_than=50>
    This product qualifies for express shipping.
  </If>

The <If> tag also now accepts two new syntax shortcuts, one to shorten conditional statements and one to make it easier to refer to variables as the subject of a condition:

  • Instead of writing out <If count more_than value="2">, you can write slightly shorter conditional statements with the syntax <If count more_than="2">.
  • Instead of using the <Get /> tag to check the value of a variable (such as <If check="{Get x}" less_than value=10>), you can now use the variable condition, which would look like this: <If variable=x less_than value=10>.

New modulo operator (%) available in the <Math> tag

The modulo operator, sometimes called the remainder, can now be used in the <Math> tag. For example, since today is May 1st, the markup below would output the phrase: "Today's date is an odd number."

  <Set date>
    <Math>
      <Date format="j" /> % 2
    </Math>
  </Set>

  Today's date is an <If variable=date value=0>even<Else />odd</If> number.

Query parameter to loop through child taxonomy terms

Many of the new features and changes contained in this update arose from discussions on our forums, so be sure to join our forum to make suggestions about the future of this product! This new feature also arose from a forum post in which a user wanted to single out only child terms in a taxonomy term loop. To accomplish that, we added the children=true query parameter which works similarly to the parents parameter and includes only child terms in the taxonomy term loop.

New field to loop through posts belonging to a taxonomy term

Another addition to the taxonomy term loop is the “posts” field. When inside a taxonomy term loop, the <Field posts /> tag can be used to get an array of post IDs that belong to the current taxonomy term. It’s always been possible to create a post loop with a terms parameter to list posts belonging to a certain taxonomy term. With the new posts loop, it’s now also possible to create a taxonomy term loop first and then loop through all posts that belong to that taxonomy term. This gives you more ways of structuring your loops based on your site’s unique needs.

  <Loop taxonomy=category name=updates>
    <strong><Field title /></strong><br/>
    <ul>
      <Loop type=post id="{Field posts}" count=5>
        <li><a href="{Field url}"><Field title /></a></li>
      </Loop>
    </ul>
  </Loop>

The markup above outputs a list of the five most recent posts in our Product Updates category:

Product Updates

A few other features in this update

Here are some of the other features and fixes that you’ll find in 2.4.1.

  • Advanced Custom Fields integration:
    • Now supports sorting and ordering posts in the relationship field
  • Calendar loop improvement and fix:
    • Now supports months written by their full name instead of just the month’s number
    • Catches an error thrown by Carbon date library if an invalid date string is given
  • Layout template type:
    • Adds location rule for matching URL route with wildcard support
  • Post loop:
    • When using an auto-generated excerpt, L&L now wraps the “read more” text in an <a> tag that links to the post
    • Now supports filtering a query by multiple fields instead of just one using the parameter variants custom_field_2, custom_field_value_2, etc.
  • Shortcode tag:
    • Adds a new debug attribute to easily inspect which tag attributes are passed
    • Improvements when using shortcodes inside tag attributes
    • New start media tags based on WordPress’s core shortcodes: Audio, Gallery, Playlist, and Video
  • Taxonomy term loop:
    • Taxonomy tag: improvements when looping through the current post’s taxonomy terms
    • Term tag: adds a new shortcut to get a field from the current taxonomy term

Leave a Reply

Your email address will not be published. Required fields are marked *