Help with hook_views_pre_build() 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?

&lt;?php
/**
 * Implements hook_views_pre_build().
 */
function custom_stories_views_pre_build(&amp;$views) {
  if($views-&gt;name == 'stories' &amp;&amp; $views-&gt;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-&gt;get_items_per_page() - $views-&gt;get_offset();
      $views-&gt;set_items_per_page($item_num);
    }
    else {
      // Remove attachment for other pages.
      $views-&gt;is_attachment = TRUE;
      // We don't need to handle the offset anymore.
      $views-&gt;set_offset(0);
    }
  }
}
EDIT

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

$views-&gt;items_per_page($item_num); instead of 
$views-&gt;set_items_per_page($item_num);


Related questios

May be this can help you