1

I have a Views-generated block which lists news. I would like the number of items to depend on the page it is displayed on - for instance: on the front page there's a place for 3, on another one - even 10. Is there a way to do that via a View tpl or template.php code?

I would rather not have an almost-identical copy of the View just for a different purpose.

  • If it's only for a couple of variants, you can clone the block display inside the same view and only override the number of items. Otherwise use hook_views_query_alter inside a custom module. – Attiks Apr 27 '12 at 18:12
5

This works for me:

function MYMODULE_views_pre_build(&$view) {
  if ($view->name == 'view_name') {
    $view->items_per_page = 5;
  }
}
  • One comment is that if there's a pager on the View, consideration has to be taken to what happens on future pages. If future pages, for example, show 10 items at a time, then 5 items will be skipped (because the pager will think that page 1 had 10 items, even though the user only saw 5). Changing $view->current_page to be one fewer and $view->offset to be the number of results on the first page seems to be the way to do this. That avoids missing results. You also have to tweak the pager to fix the numbers, but I don't have space to explain that here :) – wizonesolutions May 6 '14 at 0:16
  • Where is this function placed? In which file? – albert May 8 '18 at 16:25
  • In a module file @albert – Clive May 8 '18 at 16:40
  • So I have to do a module only to have the option of change the number of results of a view page? I don't like this option, but if there is not more solutions... – albert May 9 '18 at 16:25
2

I can think of two ways.

The best one, requires Page manager and Panels. Using those is to large a discussion to fit in here, but when a View is rendered with those tools, you can override the actual pager on a case by case basis. Page manager gives you a large set of conditions to manage when and where this happens.

The second solution uses a custom module:

function hook_views_pre_build(&$view) {
  if ($view->name == 'view_name' && $is_front) {
    $view->items_per_page = 5;
  }
}
0

In your views tpl check for page arguments or $is_front And apply limit vars to foreach or whatever loop you're using.

see Using Views templates

  • the only problem is that all data gets loaded anyway from the database, so it isn't optimal although it will work. – Attiks Apr 27 '12 at 18:07
  • You make it clear by adding order by – niksmac Apr 27 '12 at 19:45
0

Well, hook_views_pre_build should be the hook to be using if you want to alter paging, etc.. maybe you could try to post some code you've tried ?

if i recall correctly, you can set the items per page like this:

MYMODULE_views_pre_build(&$view){
$view->pager["items_per_page"] = 0;
}

use your own logic to alter the items per page as you wish.

  • Thank you, sorry I forgot to mention that I use Drupal 7 and views 3, tried your code but didn't work! – Luca May 17 '12 at 21:40

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.