Welcome to Genhike Sports

Content Query with Pagination

← Back to the tag reference

Hand-written Includes in-depth notes

Content Query with Pagination content_query Used inside components 4 host files / 4 calls

Takes the same parameters as content_list but also returns the total count and a paginator; use it on list pages that need paging.

content_query(array $options = [])

Pages List pages

Used in
List pages
Defined in
core/tpl_functions.php:291
Core method
Tpl::contentQuery()
How do I read these repository paths?

All paths are relative to the site root. Open core/tpl_functions.php at line 291 to see the definition of content_query(). The "core method" is what the tag actually forwards to: a template writes content_list(...) and the core runs Tpl::contentList(...).

1. Fill in parameters, get the code Edit any field and the result below updates instantly; press Copy and paste it into a template. It also works without JavaScript: press Generate and the server recomputes it.

click the title to collapse / expand
Model code, defaults to news; an unknown model gives empty items and a null paginator.
Current page; without it no paginator is built. Values below 1 are treated as page 1.
Rows per page, defaulting to the admin pager setting; it overrides limit with page.
Category numeric id: 3, 3,5, or [3,5] all work (core/Content.php:96-113) - a comma string is expanded by the generator in the tool, so write [3,5] when you type it into a template yourself. Ids only: a slug becomes 0 and gives an empty list, so use category_slug for slugs.
Content ids to leave out: 4, 4,6, or [4,6] (core/Content.php:116-125). As with category, the comma form is expanded by the generator; write [4,6] in a template yourself. The related block on a detail page uses it to drop the current row.
Generated code

Now generating: content_query

Real page previewOpen in new window

This is the real rendering of the page (real header, container, this tag output, real footer). It reloads when you change a parameter.

This is the server-computed default; it updates as you edit any parameter and the preview on the right is re-rendered too. It also works without JavaScript: fill the form and press Generate.

2. Full parameter table 5 parameters, one per row; scroll sideways on narrow screens.

click the title to collapse / expand
Parameter Type Default Description
model string news Model code, defaults to news; an unknown model gives empty items and a null paginator.
page int 1 Current page; without it no paginator is built. Values below 1 are treated as page 1.
page_size int From the category settings Rows per page, defaulting to the admin pager setting; it overrides limit with page.
category int|array required / as needed Category numeric id: 3, 3,5, or [3,5] all work (core/Content.php:96-113) - a comma string is expanded by the generator in the tool, so write [3,5] when you type it into a template yourself. Ids only: a slug becomes 0 and gives an empty list, so use category_slug for slugs.
exclude int|array required / as needed Content ids to leave out: 4, 4,6, or [4,6] (core/Content.php:116-125). As with category, the comma form is expanded by the generator; write [4,6] in a template yourself. The related block on a detail page uses it to drop the current row.

Apart from the category / exclude entries listed above, all remaining parameters are identical to content_list.

3. Examples Every example can be copied as-is; the later ones are more advanced.

click the title to collapse / expand

Minimal list with a pager

Always pass page, which is $current_page in a list template; rows come from $q['items'] and the pager from pager().

<?php $q = content_query([
    'model'     => 'product',
    'page'      => max(1, (int)($current_page ?? 1)),
    'page_size' => 12,
]); ?>
<ul class="ry-cards">
<?php foreach ($q['items'] as $item): ?>
    <li><a href="<?= e($item['_url']) ?>"><?= e(field($item, 'title')) ?></a></li>
<?php endforeach; ?>
</ul>
<?= pager($q['paginator']) ?>

Case list with a total count and thumbnails

total carries the count and items carries the rows; the if guard keeps rows without a thumbnail from printing broken images.

<?php $q = content_query([
    'model'     => 'case',
    'category'  => 7,
    'page'      => max(1, (int)($current_page ?? 1)),
    'page_size' => 9,
    'order'     => 'publish_time desc',
]); ?>
<p class="ry-total"><?= (int)$q['total'] ?> items</p>
<ul class="ry-cases">
<?php foreach ($q['items'] as $item): ?>
    <li>
        <a href="<?= e($item['_url']) ?>">
            <?php if ($item['_thumb_url'] !== ''): ?>
                <img src="<?= e($item['_thumb_url']) ?>" alt="<?= e(field($item, 'title')) ?>" loading="lazy">
            <?php endif; ?>
            <h3><?= e(field($item, 'title')) ?></h3>
        </a>
    </li>
<?php endforeach; ?>
</ul>
<?= pager($q['paginator']) ?>

An explicit empty state

Test items before printing the container so that an empty list still shows a message.

<?php $q = content_query([
    'model'     => 'news',
    'page'      => max(1, (int)($current_page ?? 1)),
    'page_size' => 10,
]); ?>
<?php if (!$q['items']): ?>
    <p class="ry-empty">No content yet.</p>
<?php else: ?>
    <ul class="ry-list">
    <?php foreach ($q['items'] as $item): ?>
        <li><a href="<?= e($item['_url']) ?>"><?= e(field($item, 'title')) ?></a></li>
    <?php endforeach; ?>
    </ul>
    <?= pager($q['paginator']) ?>
<?php endif; ?>

Only a few categories, minus a couple of rows

Both category and exclude take ids: a single id, 3,5, or [3,5]. Use category_slug when you only have a slug, and never pass both.

<?php $q = content_query([
    'model'     => 'product',
    'category'  => [3, 5],
    'exclude'   => [12, 13],
    'page'      => max(1, (int)($current_page ?? 1)),
    'page_size' => 12,
]); ?>

4. What it does / when to use it

Use it on list pages that need paging: it takes the same query options as content_list but also computes the total count and a paginator. Category list pages, search results, and paged product lists are all this case. For a fixed handful of items, such as a home section or a sidebar box, do not use it; without page there is no paginator, so the extra wrapper array buys you nothing.

5. Which pages it belongs on This is the question clients actually ask: "I am building the home page / a list page / a detail page — which tags do I use?"

click the title to collapse / expand
Page What that page is What this tag does there
List pages Category list pages: listing, filters, pagination On a list page it lives in the list container or right next to the pagination bar.
You use it to: Use it on list pages that need paging: it takes the same query options as content_list but also computes the total count

Clicking a page name jumps to every tag for that page in the reference (the "By page" filter there). This page always shows everything about the tag, whatever that filter is set to.

6. How to use it (step by step)

  1. Write the filters exactly as you would for content_list (model, category, category_in, order and so on). Only the return value differs: you get an array, and the rows live in $q['items'].
  2. Pass page as the current page number. List page templates already have $current_page, and max(1, (int)($current_page ?? 1)) keeps junk such as ?page=0 from breaking the query. When page is missing entirely, no paginator is built and pager() prints nothing.
  3. Pass page_size as well to decide how many rows go on a page. When it is missing, the site setting for the front-end page size is used, falling back to the older template setting and then to app.page_size, so your page will disagree with the rest of the site.
  4. Loop over $q['items'] and print pager($q['paginator']) below the list. Button wording and the page number window follow the site language and pagination settings, so you never build those links by hand.
  5. Read $q['total'] for a count such as items found, and test whether $q['items'] is empty before printing the container so that an empty result gets its own message instead of a bare box.

7. Common pitfalls

  • Passing both limit and page: when page is present the row count comes from page_size and limit is ignored. To control the rows per page, pass only page_size.
  • Passing a category slug such as news as category: the implementation casts it with (int) and gets 0 (core/Content.php:97), which yields an empty list with no error at all. Use category_slug for slugs, and write several categories as 3,5 or [3,5], which mean the same thing (core/Content.php:96-113).
  • Calling pager($q['paginator']) without passing page: paginator is null, pager() returns an empty string, and the page looks as if paging is broken when in fact it was never computed.
  • Expecting a pager when there is only one page. The paginator renders no HTML at all in that case, by design; print $q['total'] yourself if you want a count there.
  • Expecting path style links such as /zh/products/page/2 from a template call. The paginator needs a base URL to build them and the core passes one on list pages; called from your own template it falls back to the current URL plus ?page=N.
  • Looping over $q itself: it is an array of four keys (items, total, paginator, model), so a direct loop hands you nested arrays and objects, field() finds no field, and the page fills with noise.

Note from the reference table Two corrections: four keys come back (items / total / paginator / model), and page_size comes from the pager settings (then the old setting and app.page_size, ending at 12), not from category settings.

8. Real rendered output This section was rendered once on the server with this site real data; it is not a mock-up.

click the title to collapse / expand

It returns the keys items / total / paginator (items is truncated to 2 rows here to show the shape). Hand paginator to pager() to get the page bar.

Open this page in a new window →

9. Where it already appears in your templates Grouped by components / page templates / template packs; the data comes from a script scan.

click the title to collapse / expand

Components 4 files / 4 calls 89% of tag calls live in components

  • templates/_shared/components/home-about-split.php ×1 lines 88
  • templates/_shared/components/home-docs-cards.php ×1 lines 71
  • templates/_shared/components/home-docs-tree.php ×1 lines 43
  • templates/_shared/components/home-stats-bar.php ×1 lines 81

Click "See this code + preview": the left shows the real code around that call site (±12 lines, call line highlighted) and the right renders a real page preview with the current parameters.

Common combinations (generated from your real templates)

  1. Fetch the rows with this tag → render the cards with home-docs component(s)
  • templates/_shared/components/home-docs-cards.php

11. Previous / next In group order; this is tag 3 of 56.

click the title to collapse / expand
Send an Enquiry