Etch component
Facet 1
One WP Grid Builder facet by its ID, styled with the rest by the wpgb stylesheet.
Documentation
MAC Facet 1 places one WP Grid Builder facet on an Etch page or template. WP Grid Builder does the filtering and Etch draws the list with a loop. They find each other through one name that starts with wpgb-content-, which goes in three places: the loop, the list and each MAC Facet 1. One component places all 21 facet types, since a facet's type, data and labels come from its settings in WP Grid Builder.
How to use it
An example: the demo's first list on this page, cut down to one filter and Pagination. Its name is wpgb-content-hub-fillers.
1. The facets, in WP Grid Builder
In All Facets, click Add New for each facet, choose these and save:
- The filter: Facet Action Filter Content, Filter Type Checkboxes and Filter Content By Filler Categories (under Taxonomies). It got ID 5.
- Pagination: Facet Action Load Content and Load Type Pagination. It got ID 1.
A facet's Shortcode button shows its ID, as in [wpgb_facet id="5"].
2. The loop, in Etch's Loop Manager
A normal WP Query loop, named hubFillers, with one more argument that holds the name:
$query_args = [
'post_type' => 'fillers',
'posts_per_page' => 6,
'wp_grid_builder' => 'wpgb-content-hub-fillers'
];
posts_per_page is the page size that Pagination, Per Page and Load More use.
3. The structure, in Etch
<div class="fillers">
<aside>
<!-- MAC Facet 1: Facet 5, Grid wpgb-content-hub-fillers -->
</aside>
<ul class="fillers__list wpgb-content-hub-fillers">
{#loop hubFillers as item}
<li>{item.title}</li>
{/loop}
{#loop hubFillers.length().equal(0, [1], []) as empty}
<li>No results</li>
{/loop}
</ul>
<!-- MAC Facet 1: Facet 1, Grid wpgb-content-hub-fillers -->
</div>
- The name is a class on the element that holds the items directly, here the
ul, never on a wrapper around it. Etch keeps a class only while it has a style, so keep its style, even an empty one. - Each facet is a MAC Facet 1, pasted with Copy JSON above and placed outside the list, anywhere on the page. Set Facet to the facet's ID and Grid to the name.
- The second loop runs only when the first one has no items, and shows "No results". It stays inside the list, since filtering replaces only the list's content. Etch's loops guide calls this a workaround until loops get an empty state of their own.
4. Once per site, the styles
Click Copy CSS above. In Etch, open the Style Manager, go to Stylesheets, add a stylesheet named wpgb and paste it. It styles every facet type with the project's ACSS tokens, dropdown menus and the date picker included.
Good to know
- Every list on a page needs its own name, such as
wpgb-content-productsandwpgb-content-events, in lowercase, and its facets take that name as their Grid. - A facet with a Reveal Facet action in its Conditions tab, such as one that needs Facet Result Count to be at least 1, hides when nothing matches, and visitors lose the controls they'd use to undo the filter. The demo's facets have no actions.
- Some facets render nothing while they have nothing to show: Result Counts with no results, Pagination while every result fits on one page, Per Page while the results don't exceed its smallest option, and a choice facet with no choices left.
- WP Grid Builder filters from its own index, which it updates when a post or a facet is saved. If a facet shows no choices, click Index Facets in All Facets.
- Apply Filters holds every other facet of its list until it's clicked, so a list either has Apply or filters as visitors choose.
- WP Grid Builder's Filter Custom Content setting isn't needed: it's the automatic alternative to naming the content in the loop and the class, as its guide to custom queries explains.
- To filter, WP Grid Builder requests the page's own address with
?wpgb-ajax=render, without its other parameters. If a condition shows the list only for a URL parameter, let those requests through too, withurl.full.includes('wpgb-ajax='). - Restyle facets in the site's
wpgbstylesheet, not in MAC Facet 1: dropdown menus and the date picker render outside the component, at the end of the page. - Etch's own WP Grid Builder guide shows the same setup.
- To have screen readers announce a filter's results, put WP Grid Builder's Result Count facet in a
divwith the classhidden-accessibleandrole="status". Give the no-results linerole="alert", on aspan.
The demo
Beyond the example, the first list has every filter type beside it and a bar above it (User Selection, Result Counts, Sort, Per Page and Reset). Apply holds the other facets of its list until it's clicked, so it has a list of its own, with the A-Z Index and Load More. Search for something that matches nothing to see the No results item while the filters stay. Load More's infinite-scroll variant isn't shown.