Is there a way to do a dynamic offset with views?

For example.

I have a view returning the following node id's:

1, 2, 3, 4, 5

If I'm viewing node 3, I would like the view (as a block on that page) to show nodes 3, 4, 5. Eg, the current node is first i the view and the ones before are not displayed.

This would be the same as putting 3 in the "offset" option, but it is not dynamic.

Comments

JayFid’s picture

subscribing

slowflyer’s picture

subscribing

adam_b’s picture

I'm sure this is possible, but you'll need a bit of custom PHP. Beyond my abilities, I'm afraid.

wucherpfennig’s picture

sub

arielberg’s picture

function {hook}_views_pre_view(&$view, &$display_id = null , &$args = null) {
$pager = $view->display_handler->get_option('pager');
$pager['options']['offset'] = 3;
$view->display_handler->override_option('pager', $pager);
}

myselfhimself’s picture

Thanks @arielberg your piece of code was awesome.

As to me, I did set something else than 3, so as to remove double results between a view and its attachment (the view has the dynamic offset as below + a pager; the attachment has the same query as the view, with "Inherit pager : no"):

$pager['options']['offset'] = empty($args) ? 1 : 0;