$view->total_rows is wrong (sort of), I want “Item

2019-08-17 00:55发布

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));

4条回答
欢心
2楼-- · 2019-08-17 01:26

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);
查看更多
仙女界的扛把子
3楼-- · 2019-08-17 01:30

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.

查看更多
神经病院院长
4楼-- · 2019-08-17 01:45

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.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-08-17 01:51

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.

查看更多
登录 后发表回答