Skip Navigation

[Resolved] Display view depending on number of page results

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display the results of a paginated View differently depending on the number of results shown on a page.

Solution: You can use wpv-item, index, and padding with conditional HTML to manipulate the Content Template applied to each item in the loop.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-items-count
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/digging-into-view-outputs/

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by johnU 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1174768

Is it possible to display a View template depending on how many results are on the displayed page?

For example, I am using Views to display a repeatable field, split into pages of 6 results. So if there are 11 results total, the second page only has 5 to show. I would then like to use a different view (or loop?) for that second page than used for the first page which had 6 results.

I hope that made sense.

#1174888

Hi, yes it makes sense. In a View's Loop Editor, you have access to the shortcode wpv-items-count, which will tell you the number of results that will be included on the current page of results: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-items-count
Note that if you use wpv-conditional tags to hide some results in the loop, wpv-items-count is not set up to reflect that modification.

So with that in mind, you could use the wpv-items-count shortcode in conditional HTML to display a different Content Template in the Loop:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

You can also use the wpv-item, index, wrap, and pad attributes to specify markup for specific items in the loop. More information about these attributes can be found here: https://toolset.com/documentation/user-guides/digging-into-view-outputs/

#1174917

wpv-items-counts is exactly what I was looking for, although I am still having trouble displaying what I need.

For example, I put the following php code in the page template where I want the view displayed...


<?php

$args = array(
    'title' => 'View Triage Template',
    'id' => '358'
);
echo render_view( $args );

?>

...and the following in the loop editor of the mentioned, "View Triage Template", notice the "There are [wpv-items-count] results in this page" part...


[wpv-layout-start]
	[wpv-items-found]
		
		There are [wpv-items-count] results in this page

		[wpv-conditional if="( '[wpv-items-count]' eq '6' )"]
			[wpv-view name="view-template-for-6-results"]
		[/wpv-conditional]

		[wpv-conditional if="( '[wpv-items-count]' eq '5' )"]
			[wpv-view name="view-template-for-5-results"]
		[/wpv-conditional]

		[wpv-conditional if="( '[wpv-items-count]' eq '4' )"]
			[wpv-view name="view-template-for-4-results"]
		[/wpv-conditional]

		[wpv-conditional if="( '[wpv-items-count]' eq '3' )"]
			[wpv-view name="view-template-for-3-results"]
		[/wpv-conditional]

		<!-- wpv-loop-start -->
		<wpv-loop>
          
		</wpv-loop>
		<!-- wpv-loop-end -->

[/wpv-items-found]

	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]

[wpv-layout-end]

...in the other 4 view templates, I put the pagination code. And, with this set up, it correctly displays the first page with the correct number of items counted and the correct views template. But when I click to go to the next page, even if there are only 5 remaining items, it states that there are 6 results and shows the "view-template-for-6-results" view template.

Now, when I put the pagination code in the original "View Triage Template", the first page displays correctly again. And when I click to go to the next page, it shows the correct remaining number of items, and uses the correct view template. BUT, it shows the same items from the first page.

#1174921

Now, you mentioned using Content Templates in the Loop, but how would I target the items as I would in a view, ie. [wpv-item index=1]

#1175173

For example, I put the following php code in the page template where I want the view displayed
Check the documentation for render_view. You should only pass one of the three arguments "name", "title", or "id". You have included both title and id in your arguments array. If you're comfortable using PHP, you should take a look at the get_view_query_results API and other Views API hooks:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
https://toolset.com/documentation/programmer-reference/views-filters
With get_view_query_results you can inspect the results found on any page of the View, programmatically. That would give you the ability to see how many results will be displayed. Then based on that information you can show a different View's results, add different shortcode attributes to the View's arguments, or modify other View settings using other Views API hooks.

...when I put the pagination code in the original "View Triage Template", the first page displays correctly again. And when I click to go to the next page, it shows the correct remaining number of items, and uses the correct view template. BUT, it shows the same items from the first page.
Right, I'm guessing the problem here is the nested View does not automatically display the same results as the parent View. You would have to use query filters and/or shortcode attributes on the nested View so that it knows which posts it should display. I can't say for sure without knowing more about what you want to accomplish, but I don't think a nested View is the way to go here. PHP filtering is probably the most robust approach. Or, I think conditionals inside the wpv-loop tags could be used to render different Content Templates depending on the current page number, the number of items found, and possibly the wpv-item index.

Now, you mentioned using Content Templates in the Loop, but how would I target the items as I would in a view, ie. [wpv-item index=1]
The wpv-item shortcode must be placed directly in the View's Loop editor, so you can't use it within a Content Template. You would proceed from the other direction, i.e. create multiple Content Templates, one for each item index. Then place the Content Templates in their respective wpv-item blocks in the View's Loop editor.

#1175200

The Content Template solution worked perfectly once I realized I could not use [wpv-item index=1] in those templates. For basic context, my layout was divided into a left and right side with a maximum of six items; some small, some large. Depending on the amount of results, I wanted to shift how many were on each side, and what size the items were.

Here was the final solution in case this ever comes up for anyone else...

[wpv-layout-start]

	[wpv-items-found]

		<div class="gallery-wrap"> 
		    <div class="container"> 

				<div class="gallery-section">

					<div class="gallery-left">
				
						<!-- wpv-loop-start -->
						<wpv-loop>

			      [wpv-item index=1]

						[wpv-post-body view_template="ct-gallery-large"]
						
						[wpv-conditional if="( '[wpv-items-count]' lt '6' )"]
					</div><!-- gallery-left -->

					<div class="gallery-right">
						[/wpv-conditional]

						[wpv-item index=2]

						[wpv-conditional if="( '[wpv-items-count]' gt '2' )"]
						[wpv-post-body view_template="ct-gallery-small"]
						[/wpv-conditional]
							
						[wpv-conditional if="( '[wpv-items-count]' lt '3' )"]
						[wpv-post-body view_template="ct-gallery-large"]
						[/wpv-conditional]
							
						[wpv-item index=3]

						[wpv-post-body view_template="ct-gallery-small"]
						
						[wpv-conditional if="( '[wpv-items-count]' gt '5' )"]
					</div><!-- gallery-left -->

					<div class="gallery-right">
						[/wpv-conditional]
							
						[wpv-item index=4]

						[wpv-post-body view_template="ct-gallery-small"]
							
						[wpv-item index=5]

						[wpv-post-body view_template="ct-gallery-small"]
							
						[wpv-item index=6]

						[wpv-post-body view_template="ct-gallery-large"]
                      	      
					</wpv-loop>
					<!-- wpv-loop-end -->

				</div><!-- gallery-right -->          

			</div><!-- gallery-section -->

		</div><!-- container -->
	</div><!-- picture-gallery-wrap -->

	<div class="gallery-pagination-area"> 
		<div class="container"> 
			<div class="row"> 
				[wpv-pagination][wpv-pager-nav-links previous_next_links="true" force_previous_next="true"][/wpv-pagination]
			</div>
		</div>
	</div>

	[/wpv-items-found]

	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]

[wpv-layout-end]

Christian, thank you so much for your help. I hope your holidays have been merry and I hope you have a wonderful new year!