Skip Navigation

[Resolved] Number loop like for i=1 to 10

This support ticket is created 8 years, 6 months 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
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Khurram 8 years, 5 months ago.

Assisted by: Waqas.

Author
Posts
#346897

I am trying to:

Hi Waqas,

I am using custom post type. In my CPT there are two numeric fields say 'from' and 'to'. These two fields contains image numbers from and to, for example 1 to 10, 133 to 137 etc.. I want to loop between these fields to show images. Is it possible using <wpv-loop> in views. I was guessing that if <wpv-loops> work with [wpv-condition] some how and I can use it outside [wpv-items-found].

Thanks,
Khurram

#347084

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Unfortunately, there's no such built-in solution to create a custom loop, based on this. Although View's [wpv-for-each] (https://toolset.com/documentation/views-shortcodes/#wpv-for-each) offers such a thing, but that's for multiple items in a custom field (or repeating fields). Since you want to loop through the numbers from 2 different fields, you will need a custom solution for this.

For example, following sample code creates a custom short code to accept the start/end numbers for a loop and iterates through the items:

add_shortcode("my-loop", "my_loop");
function my_loop($atts) {
    $from = $atts["from"];
    $to = $atts["to"];
    $imgURL = $atts["imgurl"];
    $output = "";

    for($i = $from; $i < $to; $i++) {
        $output .= "<img src='" . $imgURL . "' width='100' height='100' />";
    }

    return $output;
}

Add the above function in your theme's functions.php file and use it in your view as below:

[my-loop from="[types field='from'][/types]" to="[types field='to'][/types]" imgurl="[types field='my-image-field'][/types]"]

And this will output the "imgurl" as per the iterations defined by "from" and "to" fields.

I hope you can alter the above custom code as per your needs. Please let me know if I can help you with anything related.

#349031

Thanks Waqas, it works 100%.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.