We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10473
    • 44 Posts
    I looked around the forums and couldn’t find a solution to my problem, so I decided the roll up my sleeves and make my own solution.

    Problem: using the pages tag displays all pages when using pagination, and I only want to show a maximum of 7 (the current page, 3 below and 3 above).

    The solution (written in 0.9.6.3 with ditto 2.1):

    Step 1. Add the following parameter definition to the snippet code in the manager:

    $pagesPerPage = isset($pagesPerPage) ? $pagesPerPage : 0;
    /*
    	Param: pagesPerPage
    
    	Purpose:
     	Number of pages to display in pagination
    	
    	Options:
    	Any number
    
    	Default:
    	0
    */
    


    Step 2: Also in the snippet code, add this parameter to $ditto->paginate call:
    		$ditto->paginate($start, $stop, $total, $display, $tplPaginateNext, $tplPaginatePrevious, $tplPaginateNextOff, $tplPaginatePreviousOff, $tplPaginatePage, $tplPaginateCurrentPage, $paginateAlwaysShowLinks, $paginateSplitterCharacter, $pagesPerPage);


    Step 3: Change the parameter list in the paginate function definition in assets/snippets/ditto/classes/ditto.class.inc.php:
    	function paginate($start, $stop, $total, $summarize, $tplPaginateNext, $tplPaginatePrevious, $tplPaginateNextOff, $tplPaginatePreviousOff, $tplPaginatePage, $tplPaginateCurrentPage, $paginateAlwaysShowLinks, $paginateSplitterCharacter, $pagesPerPage) {
    


    Step 4: In the same file, replace the code:
    		for ($x = 0; $x <= $totalpages -1; $x++) {
    			$inc = $x * $summarize;
    			$display = $x +1;
    			if ($inc != $start) {
    				$pages .= $this->template->replace(array('url'=>$this->buildURL("start=$inc"),'page'=>$display),$tplPaginatePage);
    			} else {
    				$modx->setPlaceholder($dittoID."currentPage", $display);
    				$pages .= $this->template->replace(array('page'=>$display),$tplPaginateCurrentPage);
    			}
    		}
    


    with:

        $begin = 0;
        $end = $totalpages - 1;
        $tmpCurrentPage = $start / $summarize;
        $pagesPrefix = '...';
        $pagesPostfix = '...';
        if ($pagesPerPage && $pagesPerPage < $totalpages)
        {
          if ($tmpCurrentPage < $pagesPerPage / 2)
          {
            $end = $pagesPerPage - 1;
          }
          elseif ($tmpCurrentPage > $totalpages - ($pagesPerPage / 2))
          {
            $begin = $totalpages - $pagesPerPage;
          }
          else
          {
            $begin = ceil($tmpCurrentPage - ($pagesPerPage / 2));
            $end   = floor($tmpCurrentPage + ($pagesPerPage / 2));
          }
        }
        if ($begin <= 0)
        {
            $pagesPrefix = '';
        }
        if ($end >= $totalpages-1)
        {
            $pagesPostfix = '';
        }
    
    		for ($x = $begin; $x <= $end; $x++) {
    			$inc = $x * $summarize;
    			$display = $x +1;
    			if ($inc != $start) {
    				$pages .= $this->template->replace(array('url'=>$this->buildURL("start=$inc"),'page'=>$display),$tplPaginatePage);
    			} else {
    				$modx->setPlaceholder($dittoID."currentPage", $display);
    				$pages .= $this->template->replace(array('page'=>$display),$tplPaginateCurrentPage);
    			}
    		}
    		$pages = $pagesPrefix . $pages . $pagesPostfix;
    


    Step 5: Use it. You can now use the parameter pagesPerPage as in the following snippet call.

    [!Ditto? &sortBy=`pub_date` &sortDir=`DESC` &summarize=`5` &paginate=`1` &paginateAlwaysShowLinks=`1` &pagesPerPage=`7` !]



      • 25951
      • 9 Posts
      I just wanted to say thanks a lot. smiley This worked for me and is way better then every other solution I found in the forums. Why could I not have found it earlier... spent a lot of time searching for a solution.

      You should talk to the Ditto people if they don’t want to implement your solution for a further version right into the snippet.
        • 10473
        • 44 Posts
        Quote from: Sajonara at Sep 17, 2009, 06:31 PM

        I just wanted to say thanks a lot. smiley This worked for me and is way better then every other solution I found in the forums. Why could I not have found it earlier... spent a lot of time searching for a solution.

        You should talk to the Ditto people if they don’t want to implement your solution for a further version right into the snippet.

        Thanks for the appreciation. I have a few other nice additions to the MODx core at modx.cmsthingies.com. Also, any suggestions are gratefully accepted.
          • 18206
          • 59 Posts
          Looks really neat. Things like these should get rolled back into the core IMO..
            • 10473
            • 44 Posts
            Quote from: Mar at Nov 17, 2009, 12:56 PM

            Looks really neat. Things like these should get rolled back into the core IMO..

            Thanks for the positive comment. It’s really appreciated.
              • 7222
              • 20 Posts
              Thanks for this, however:

              If I use &start=`1` to skip the first article, the pagination will show a final page which displays "no results found".

              Any suggestions how to take ’start’ into consideration when calculating the number of pagination links.
                • 40024
                • 72 Posts
                Excellent work!I am using it myself wink . One question though,since i don't know about php,could someone modify these lines of code as to include "First" and "Last" page links?

                If pagination is too long,a user might want to return to the first page or go straight to the last.
                Any help would be greatly appreciated.
                  • 16278
                  • 928 Posts
                  I haven't tried it, but it's probably worth looking at splitPagination in the Extras. Aren't [1] and [N=biggest number] already there as first and last links?

                  ;) KP
                    • 40024
                    • 72 Posts
                    Thnk you KP for your response. smiley
                    Yep,they are.I have already tried splitpagination actually,and i didn't manage to get it to work.
                    I gave a shot at these lines of code,because 1)no one had problems with it,and 2)i prefered a solution that extends ditto's functionality,rather than having a new snippet connecting to ditto.