Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
How can we set the items per page to a view, programatically?
The similar code we are using in drupal 5 is as follows:
$view = views_get_view('visual_articles');
print (views_build_view('embed', $view, array(0 => '1', 6 => 'video'), $view->use_pager, $view->nodes_per_block));
Can u please give me the corresponding code in druapal 6
Thanks in advance.
Cibi
Comments
Comment #1
zorroposada CreditAttribution: zorroposada commentedI had the same question.
I found useful info here:
http://pedroposada.com/blog/embed-views-2-nodes.html
Comment #2
alienzed CreditAttribution: alienzed commented$args = array();
$view = views_get_view('viewName');
$view->set_display('default');
$view->display_handler->set_option('items_per_page', 1);
print $view->preview('default', $args);
nothing but the above code here worked for me
replace viewName by your view name and voila!
Comment #3
alienzed CreditAttribution: alienzed commentedif you want to do the same thing without embedding the view, you'll need to create a module (trust me, there's no other way)
then use hook_views_pre_execute()
Comment #4
Rob T CreditAttribution: Rob T commentedThanks so much, alienzed. After a few hours of frustration, stumbling into your solution in #2 worked perfectly.
Comment #5
qlmhuge CreditAttribution: qlmhuge commentedAnd where does one put this snippet of code? In a block, in a node, in a view, in a template? Sorry, I'm not strong on the coding side, but I would really like my users to set the number of results per page in a view. Can someone give me the "Dummies" version?
Comment #6
daboo CreditAttribution: daboo commentedWell, I can tell you, don't put it into a block and apply it to the whole site. It will take your site down if it does not work. Believe me, I just learned the hard way. Thank goodness it was as simple as replacing the field content in the db to get it back.
If I figure out where to put it that makes logical sense and works, I'll be the first to share my results.
Best Regards,
Daboo
Comment #7
dkinzer CreditAttribution: dkinzer commentedI found what I think is a much easier way of pulling this off if Views_3 is not an option yet.
using hook_form_alter apply the following to the appropriate $form:
//Add select number of pages filter.
$pager_options = array(20, 30, 50);
$form['views_page_filter'] = array(
'#type' => 'select',
'#options' => $pager_options,
'#default_value' => 1,
'#title' => t('Select items per page'),
);
$pages = $pager_options[$form_state['input']['views_page_filter']];
$form_state['view']->set_items_per_page($pages);
Though this will only work if you have an exposed filter for the view.
Comment #8
Vendict CreditAttribution: Vendict commentedIt works, thank you!