I have set up a View to show a list of stories, and then set up an attachment to my View that puts the first 4 stories at the top of the View styled differently. I need those 4 to only show on the first page of items. Every other page will show the normal styling without the top 4. I have created a custom module using the following code. However, now every item is showing on one page. I have set number of items for the pager in the main view to 10 with an offset of 4, and in the attachment it is set to 4. Any suggestions?

 * Implements hook_views_pre_build().
function custom_stories_views_pre_build(&$views) {
  if($views->name == 'stories' && $views->current_display == 'block_1') {
    // if 'attachment_1' is not defined, we assume it is the first page.
    if (empty($_GET['attachment_1'])) {
      // The offset number y should be equal to the number of items on
      // attachment. If we want to keep the total n items on list, the
      // number of items on main display should be (n - y).
      $item_num = $views->get_items_per_page() - $views->get_offset();
    else {
      // Remove attachment for other pages.
      $views->is_attachment = TRUE;
      // We don't need to handle the offset anymore.
  • um... why not ask in the old post? And it's obviously my solution for that. What is your question this time?
    – Jimmy Ko
    Commented Jul 22, 2016 at 16:38
  • Thanks @Jimmy Ko. I had responded in the chat you mentioned. I just followed up with you again.
    – kayzoe
    Commented Jul 22, 2016 at 17:00
  • No worry, I am in chat now.
    – Jimmy Ko
    Commented Jul 22, 2016 at 17:06
  • 5
    Solved - drupal.stackexchange.com/questions/207701/…
    – kayzoe
    Commented Jul 22, 2016 at 17:08

1 Answer 1



You can try the below code its working fine for me.

$views->items_per_page($item_num); instead of 

Related questios

May be this can help you

  • $views->set_items_per_page() indeed exists in views-7.x-3.13.
    – Jimmy Ko
    Commented Jul 22, 2016 at 16:46
  • May be , i am not sure about it , so i updated my answer
    – bisw
    Commented Jul 22, 2016 at 17:19

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