Skip Navigation

[Resolved] access to [wpv-item index=1] in content template in loop

This support ticket is created 6 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by darcyC-2 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1131807

Tell us what you are trying to do?

I need my content template to have a conditional for when it is first in a loop. I see the shortcode [wpv-item index=1], but this is only available within the view loop and not within the content template being accessed via [wpv-post-body]

What is the best way to achieve what I am hoping to do?

#1131979

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Darcy

There isn't a way to readily communicate to the linked template what the current iteration of the loop is, that is really only accessible within the wpv-loop tags.

I can think of how you might do it with custom shortcodes, but first, can you tell me what you are trying to do?

Maybe it can be done somehow from within the Loop Editor.

#1132294

I would like to know within my content template whether I am the first item in the loop, so I can render a bigger image.

#1132670

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2018-10-23 at 13.47.33.png

If it's a question of CSS styling then you can output the View in such a way that the content for each iteration has a wrapper div, only the first of which has a custom class.

Use the wpv-item shortcode to add the custom class to the initial iteration, like so:

	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          [wpv-item index=1]
          <div class="first">
			[wpv-post-body view_template="Loop item in Highlight first listing"]
          </div>
          [wpv-item index=other]
          <div>
			[wpv-post-body view_template="Loop item in Highlight first listing"]
          </div>          
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
#1132979

Seems like a shame that I cannot put logic into the content template. Can I define a global variable or pass a variable with the wpv-post-body tag?

#1133359

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I have provided a generic shortcode for working with View iterations before which I think you should be able to use:

/**
 * Add loop-iteration shortcode
 * 
 * Attributes
 * 'n' (optional) : test for nth iteration
 */
add_shortcode( 'loop-iteration', function( $atts ){

	global $loop_n;

	if ( !isset( $loop_n ) ) {
		$loop_n = 0;
		return $loop_n;
	}

	$loop_n++;

	if ( !isset( $atts['n'] ) ) {
		// no nth parameter, just return the current index
		return $loop_n;
	}
	else {
		$remainder = ( $loop_n + 1 ) % $atts['n'];
		return ( $remainder == 0 );
	}

});

The intended context is with the Loop Editor, but I think it should work within a linked template referenced by the loop.

You can output the iteration, or you can use it within a conditional shortcode, which is what I suspect you need:

[wpv-conditional if="( '[loop-iteration n='1']' eq '1' )"]
  <p>1st item in loop</p>
[/wpv-conditional]

You'll need to register the shortcode at Toolset > Settings > Front-end content for it to work.

#1135740

Thank you for the solution.

It might be nice is Toolset incorporates the loop logic into content templates used within a loop. It would sure make sense.

Darcy