2

I am having below code to embed a view

$view=views_get_view('questions');
$view->set_display('block');

// execute view
$view->execute();
$viewArray = $view->result;

How do I set an offset in this? So that I can get the content I need?

I will be displaying a set of content in a page after processing it. I will need to skip first contents sometime. Like skip first 1 sometime or first 3 some other time.

2
  • 1
    | How do I set an offset in this? So that I can get the content I need? What do you mean by offset? What content are you trying to retrieve? Are you just trying to return the block display of the view?
    – acouch
    Feb 8, 2012 at 15:58
  • I have updated the question. Hope it explains it.
    – esafwan
    Feb 8, 2012 at 16:03

3 Answers 3

3

| skip first 1 sometime or first 3 some other time.

Is there a reason you can't do this in the view? If you use a pager you can specify an on offset in pager options.

If you need to do this in code, you do something like:

$view=views_get_view('questions');
$view->display['block']->display_options['pager']['options']['offset'] = 'OFFSET NUMBER';
$view->execute_display('block'));
1

You can do it in that way:

$view = views_get_view('testimonials');
$view->set_display('block');
$view->set_offset($offset);
$view->set_items_per_page(2);
$content = $view->preview();
0

You can also do this in a custom module using hook_view_pre_view() like so:

/**
 * Implementation of hook_views_pre_view().
 */
function MODULENAME_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'whatever' && $display_id == 'something') {
    $view->set_offset(5);
    $view->set_items_per_page(10);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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