1

I want my URLs to be like the following: domain.com/blog/2

Currently they are: domain.com/blog/P6

RIght now if I show 6 articles per page, my pagination will jump by 6.

For instance, Page 1 will be /, page 2 will be /P6/, page 3 will be /P9/, ect.

  • 2
    I guess this is not possible in current EE pagination system – Mufi - Amici Infotech Feb 14 '18 at 9:41
  • So the most common and practical URL structure isn't possible with EE? That's insane. There has to be a way to achieve it. Who the hell has P3, P6, P9, ect for their URLs? What were they thinking? – good_afternoon Feb 14 '18 at 14:56
  • They thinking to set offset in URL so they can easily get next batch of data. So P10 id pagination where P stands for pagination and 10 is an Offset. – Mufi - Amici Infotech Feb 15 '18 at 8:43
  • Yes I understand it's an offset. I'm saying it's stupid to have URLs like that. – good_afternoon Feb 15 '18 at 13:55
1

I was able to accomplish this by doing the following:

  • On index template, set Allow PHP? to Yes
  • On index template, set PHP Parsing Stage to Input
  • On single template, set Template route override to /blog/{url_title:regex[(((?!(\d+|category\/)).)+?)]} (make sure to swap blog for your index page url)

Inside index template, use the following:

<?php $entriesperpage = 6; ?>
      {if segment_2}
        <?php
        $entries = ee('Model')->get('ChannelEntry')
         ->filter('channel_id', 4) // or whatever you want/need to filter on
         ->filter('status', 'IN', array('open', 'other_status'))
         ->order('title', 'ASC')
         ->all();
         $totalentries = count($entries);
         if ( ($totalentries / (({segment_2} - 1) * $entriesperpage)) < 1 ) {
           $redirect = "TRUE";
         } else {
           $redirect = "FALSE";
         }
         $totalpages = ceil($totalentries / $entriesperpage);
         ?>
         {if <?php echo $redirect; ?>}
            {redirect='404'}
         {/if}
      {/if}
      {exp:channel:entries channel="articles" dynamic="no" {if segment_2} offset="<?php echo ({segment_2} - 1) * $entriesperpage; ?>" {/if} limit="<?php echo $entriesperpage; ?>" paginate="bottom"}
        <a class="post" href="{route='blog/single' url_title='{url_title}'}"></a>
      {/exp:channel:entries}
      <div class="pagination">
        {if segment_2}
          <?php $prevpage = {segment_2}-1; ?>
          <a href="{site_url}blog<?php if ($prevpage != 1) { echo '/' . $prevpage; } ?>" class="page-prev">Prev</a>
        {/if}
        <?php for ($i = 1; $i <= $totalpages; $i++) { ?>
          <a href="{site_url}blog<?php if ($i != 1) { echo '/' . $i; } ?>" class="{if (!segment_2 && (<?php echo $i; ?> == 1)) || (segment_2 == <?php echo $i; ?>)} active{/if}"><?php echo $i; ?></a>
        <?php } ?>
        {if !segment_2}
          <a href="{site_url}blog/2" class="page-next">Next</a>
        {if:else}
          {if segment_2 != <?php echo $totalpages; ?>}
          <a href="{site_url}blog/<?php echo ({segment_2} + 1); ?>" class="page-next">Next</a>
          {/if}
        {/if}
      </div>

This will produce the following urls:

  • website.com/blog
  • website.com/blog/2
  • website.com/blog/3
  • etc

Make sure to set $entriesperpage to your desired entries per page, channel_id to the correlating ID of channel you're displaying, and update blog to your index page url.

We are essentially using our own pagination logic, and I'm sure this could be cleaned up a bit.

Note that this will display all page links, so you may want to add custom logic if dealing with many pages.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.