0

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?

<?php
/**
 * 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();
      $views->set_items_per_page($item_num);
    }
    else {
      // Remove attachment for other pages.
      $views->is_attachment = TRUE;
      // We don't need to handle the offset anymore.
      $views->set_offset(0);
    }
  }
}
  • um... why not ask in the old post? And it's obviously my solution for that. What is your question this time? – Jimmy Ko Jul 22 '16 at 16:38
  • Thanks @Jimmy Ko. I had responded in the chat you mentioned. I just followed up with you again. – kayzoe Jul 22 '16 at 17:00
  • No worry, I am in chat now. – Jimmy Ko Jul 22 '16 at 17:06
  • 5
    Solved - drupal.stackexchange.com/questions/207701/… – kayzoe Jul 22 '16 at 17:08
0

EDIT

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

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

Related questios

May be this can help you

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

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