问题
The "total_rows
" property of my view returns the total rows (22), but not the number of items I have set the view to display (5) (through the Web UI Block->Basic Settings).
How can I get this in a proper way without relying on (the possibly soon to be deprecated "result
" property?
for now Im using
$totalRows = min($view->total_rows, count($view->result));
回答1:
This is always available as:
$view->display['your_display']->display_options['items_per_page']
where your_display
is replaced by the views display you are using (e.g. page_1
, block_1
, etc). This is available at all stages of building process.
回答2:
Does $view->pager["items_per_page"]
fit your needs? I am not 100% sure when this is available, but I am using it in a hook_views_pre_build()
in a custom module.
回答3:
By default the the result is rendered for the "default" display. If you want it for a particular display then you can use
$view = views_get_view('MY_VIEW_NAME');
$view->set_display('MY_DISPLAY'); // like 'block_1'
$view->render();
print sizeof($view->result);
回答4:
Here is a good solution that allows to display the total results (works with and without a pager) and also, the number of results on a current page (for example, on page 2 of 10): http://www.midwesternmac.com/blogs/jeff-geerling/views-show-showing-x-x-x
Be warned that this solution doesn't work for block type display, only for a page.
来源:https://stackoverflow.com/questions/3975160/view-total-rows-is-wrong-sort-of-i-want-items-to-display