Pagination is not working

hi! I want to use pagination… but when I click next, prev button, it just go to same url… not with page number. can anybody help me…?

<?php snippet('works/header') ?>
<?php 
    $tag = param('tags');
    $works = page('works');
    $items = null;
    if ($works) {
        $items = $works->children()->listed();
    }
    if ($tag) {
        $items = $items->filterBy('category', $tag, ',');
    }
?>
<section class="container">

<?php if ($works): ?>
<?php if ($items && $items->isNotEmpty()): ?>

    <section class="list-view" id="list-view">
        <?php foreach ($items as $item): ?>
            <a href="<?= $item->url() ?>">
                <div class="list-des">
                    <div class="list-view-title"><?= $item->title() ?></div>
                    <div class="list-view-category"><?= implode(' / ', $item->category()->split(',')) ?></div>
                </div>
            </a>
        <?php endforeach ?>
    </section>

    <section class="works-images" id="works-images">
        <?php foreach ($items->paginate(5) as $item): ?>
            <a href="<?= $item->url() ?>"> 
                <div class="works-image-thumb">
                    <?php
                    $coverImage = $item->cover()->toFile();
                    $resizedImage = $coverImage->resize(2000);
                    ?>
                    <img src="<?= $resizedImage->url() ?>" alt="<?= $item->title() ?>">
                    <div class="work-des">
                        <h2><?= $item->title() ?></h2>
                        <p><?= implode(' / ', $item->category()->split(',')) ?></p>
                    </div>
                </div>
            </a>
        <?php endforeach ?>
    </section>

    <section class="m-works-images" id="m-works-images">
        <?php foreach ($items as $item): ?>
            <a href="<?= $item->url() ?>"> 
                <div class="works-image-thumb">
                    <img src="<?= $item->mobileCover()->toFile()->url() ?>" alt="<?= $item->title() ?>">
                    <div class="work-des">
                        <h2><?= $item->title() ?></h2>
                        <p><?= implode(' / ', $item->category()->split(',')) ?></p>
                    </div>
                </div>
            </a>
        <?php endforeach ?>
    </section>

    <a href="<?= $item->pagination()->nextPageURL() ?>">prev</a>
    <a href="<?= $item->pagination()->prevPageURL() ?>">next</a>

</section>
<?php endif ?>
<?php endif ?>
</section>

<?php snippet('works/footer') ?>

I think it should be like that:

$items      = $items->paginate(5);
$pagination = $items->pagination();

<?php foreach ($items as $item): ?>
  ...
<?php endforeach ?>

<a href="<?= $pagination->nextPageURL() ?>">prev</a>
<a href="<?= $pagination->prevPageURL() ?>">next</a>

thanks…! but then they said… Undefined variable $items…

You’ve already $items variable in top of the codes:

<?php 
    $tag = param('tags');
    $works = page('works');
    $items = null;
    if ($works) {
        $items = $works->children()->listed();
    }
    if ($tag) {
        $items = $items->filterBy('category', $tag, ',');
    }

    $items      = $items->paginate(5);
    $pagination = $items->pagination();
?>

<?php foreach ($items as $item): ?>
  ...
<?php endforeach ?>

<a href="<?= $pagination->prevPageURL() ?>">prev</a>
<a href="<?= $pagination->nextPageURL() ?>">next</a>

In any case this would need another if statement, because $items is defined but can be null, then you would call paginate on null, or you have to inititiate the variable with a pages collection:

$items = new Pages();
<?php 
    $tag = param('tags');
    $works = page('works');
    $items = new Pages();
    if ($works) {
        $items = $works->children()->listed();
    }
    if ($tag) {
        $items = $items->filterBy('category', $tag, ',');
    }

    $pagination = $items->pagination()
?>
<section class="container">

<?php if ($works): ?>
<?php if ($items && $items->isNotEmpty()): ?>

    <section class="list-view" id="list-view">
        <?php foreach ($items as $item): ?>
            <a href="<?= $item->url() ?>">
                <div class="list-des">
                    <div class="list-view-title"><?= $item->title() ?></div>
                    <div class="list-view-category"><?= implode(' / ', $item->category()->split(',')) ?></div>
                </div>
            </a>
        <?php endforeach ?>
    </section>

    <section class="works-images" id="works-images">
        <?php foreach ($items->paginate(5) as $item): ?>
            <a href="<?= $item->url() ?>"> 
                <div class="works-image-thumb">
                    <?php
                    $coverImage = $item->cover()->toFile();
                    $resizedImage = $coverImage->resize(2000);
                    ?>
                    <img src="<?= $resizedImage->url() ?>" alt="<?= $item->title() ?>">
                    <div class="work-des">
                        <h2><?= $item->title() ?></h2>
                        <p><?= implode(' / ', $item->category()->split(',')) ?></p>
                    </div>
                </div>
            </a>
        <?php endforeach ?>
    </section>

    <section class="m-works-images" id="m-works-images">
        <?php foreach ($items as $item): ?>
            <a href="<?= $item->url() ?>"> 
                <div class="works-image-thumb">
                    <img src="<?= $item->mobileCover()->toFile()->url() ?>" alt="<?= $item->title() ?>">
                    <div class="work-des">
                        <h2><?= $item->title() ?></h2>
                        <p><?= implode(' / ', $item->category()->split(',')) ?></p>
                    </div>
                </div>
            </a>
        <?php endforeach ?>
    </section>


    <a href="<?= $item->pagination()->nextPageURL() ?>">prev</a>
    <a href="<?= $item->pagination()->prevPageURL() ?>">next</a>

thank you for answer…
but still not working…

You should use my and @texnixe answers together.