Jump to content

Pagination and urlSegments


Lance O.
 Share

Recommended Posts

I'm working on a simple blog that requires date-based archives and I'm having a difficult time wrapping my head around pagination with urlSegments.

I've modified Marty's code from this post: http://processwire.com/talk/topic/1356-news-section-archives/?p=12313

My modified code:

$thisyear = date("Y");
$year = (int) $input->urlSegment1;
$blog = $pages->find( "parent=/blog/, template=post, limit=2, sort=-date, date>=$year-01-01, date<=$year-12-31" );
foreach ( $blog as $blogitem ) {
    $str = "{$blogitem->date}";
    $publishyear = strtotime( $str );
    $urlyear = date( "Y", $publishyear );
    echo "<h2><a href='{$blogitem->url}'>{$blogitem->title}<span class='date'>{$blogitem->date}</span></a></h2>";
    echo "<p class='summary'>{$blogitem->summary}</p>";
}

My code for pagination:

$pagination = $blog->renderPager(array(
    'numPageLinks' => '10', // Default: 10
    'getVars' => '', // Default: empty
    'baseUrl' => '', // Default: empty
    'listMarkup' => "{out}",
    'itemMarkup' => "{out}",
    'linkMarkup' => "<a href=\"{url}\">{out}</a>",
    'nextItemLabel' => 'Next',
    'previousItemLabel' => 'Previous',
    'separatorItemLabel' => '',
    'separatorItemClass' => '',
    'nextItemClass' => '',
    'previousItemClass' => '',
    'lastItemClass' => '',
    'currentItemClass' => 'current'
));
echo '<div id="posts-nav">' . $pagination . '</div>';

My blog structure looks like this:

http://domain.com/blog/archive/2010/11/

And should look like this when paginated:

http://domain.com/blog/archive/2010/11/page2/

But instead looks like this:

http://domain.com/blog/archive/page2

The year and month segments aren't being written into the pagination links. I've read the documentation on pagination and scoured the forum for an example of how to get this to work, but I haven't had any luck and am at a loss at this point. I'd really appreciate it if someone could point me in the proper direction.

Link to comment
Share on other sites

Try removing baseUrl from your renderPager options array. If you set it to an empty string, as you currently have, it will ignore your URL segments.

The documentation says the default is "blank", which could lead one to believe it means an empty string or null. I quickly peeked under the hood of the module, and it seems the default is not really empty/blank; rather, it seems to detect the full current page URL including segements, so you can just omit it completely in the options.

  • Like 5
Link to comment
Share on other sites

Thank you, kkoala! That tiny little modification did the trick. I'd rub your fuzzy little belly if I could!  ^_^

Hopefully the documentation can be updated so no one else has to experience the same issue.

Link to comment
Share on other sites

  • 3 months later...

For those working with pagination and several urlSegments. This seems to be pretty bulletproof:

$currentUrl = $page->url.$input->urlSegmentsStr."/";
$pagination = $results->renderPager(array('baseUrl' => $currentUrl));
  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...