Clean URLs for nodes paginated using Paging module
标签: Paging module using Clean URLs 2010-04-23 04:30A
limitation of Drupal's pager system, that comes as a disappointment to some SEO
"freaks"
is the separate
"page" query in the URLs to browse multiple pages. Say, a third page on a
paginated node would have
is the separate
"page" query in the URLs to browse multiple pages. Say, a third page on a
paginated node would have node/1?page=0,2 in the URL.
Instead here's a method to accomplish a better looking version (like node/1/page/3).
"Paging Sweet Urls" is a module that converts the "page/X" part in the
URL into the actual "page" query that the pager system understands. We
also need to override theme_pager_links() to set proper hyperlinks in pager
navigation. Still this implementation has a limitation that it cannot
handle the functionality with aliased URLs (see below). I believe, it'll
need a mod_rewrite apache directive to be able to handle URL aliases.
But I'll take up with it some other time.
Also, there's Clean Pagination module providing similar functionality, but not for node pages. It can, however, handle views, etc. URLs. Find the code and instructions ahead.

Download "Paging Sweet Urls" module
Contents of paging_sweet_urls.info:
name = Paging Sweet Urls
description = Clean URLs for paging module.
core = 6.x
Contents of paging_sweet_urls.module:
<?php
/**
* Implementation of hook_init().
*/
function paging_sweet_urls_init() {
$args = arg();
// We use array_pop() instead of arg(x), because the url query can be
// node/X/page/2 or node/X/view/page/2 (Default local task URL for nodes).
$page_no = array_pop($args) - 1;
$page = array_pop($args);
// Check if we are on the node/X or node/X/page/X path or node/X/view/page/X paths.
if ($args[0] == 'node' && is_numeric($args[1]) && ($args[2] != 'view' || $args[2] != 'page') && $page == 'page') {
$_GET['q'] = implode('/', $args);
$pages = explode(',', $_GET['page']);
$pages[0] = !empty($pages[0]) ? $pages[0] : 0;
$pages[1] = $page_no < 0 ? 0 : $page_no;
$_GET['page'] = implode(',', $pages);
}
}
Append this function to the theme's template.php file.
function phptemplate_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) {
$args = arg();
$page_no = array_pop($args);
$page = array_pop($args);
// Check if we are not on the node/X or node/X/page/X path or node/X/view/page/X paths.
// Still using arg(x) for some checks to avoid popped $args for certain conditions.
if ((arg(0) != 'node' || !is_numeric(arg(1))) && (arg(2) != 'view' || (!empty($page) && $page != 'page')) && $element != 1) {
// Don't mingle when the required module is absent.
if (!module_exists('paging_sweet_urls')) {
return theme_pager_link($text, $page_new, $element, $parameters, $attributes);
}
}
$page = isset($_GET['page']) ? $_GET['page'] : '';
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
// $parameters['page'] = $new_page;
}
$query = array();
if (count($parameters)) {
$query[] = drupal_query_string_encode($parameters, array());
}
$querystring = pager_get_querystring();
if ($querystring != '') {
$query[] = $querystring;
}
// Set each pager link title
if (!isset($attributes['title'])) {
static $titles = NULL;
if (!isset($titles)) {
$titles = array(
t('« first') => t('Go to first page'),
t('‹ previous') => t('Go to previous page'),
t('next ›') => t('Go to next page'),
t('last »') => t('Go to last page'),
);
}
if (isset($titles[$text])) {
$attributes['title'] = $titles[$text];
}
else if (is_numeric($text)) {
$attributes['title'] = t('Go to page @number', array('@number' => $text));
}
}
$nid = arg(1);
$page_no = $page_new[1] + 1;
return l($text, "node/$nid/page/$page_no", array('attributes' => $attributes, 'query' => count($query) ? implode('&', $query) : NULL));
}
Download
24 Comments
improvement
you can override the theme function in the module, no need to modify the theme to 'complete' the module's work.
http://www.lullabot.com/articles/overriding-theme-functions-in-modules


评论