Math calculation on {{dc:looper:count}}

I have a ACF gallery that is running as a looper provider, I know that I can output the total count of the images by placing {{dc:looper:count}} in a headline element.

However I am trying to subtract that total by a specific value (3 to be exact) and display that final value in the headline. What would be the easiest way to go about that so the output is dynamic if the image count varies (I am not worried about an negative number)?

Hello @designerken,

Thanks for writing in! Regretfully we do not have a feature that can perform calculations with the looper count. I would recommend that you create a custom PHP function or shortcode to be able to do that. This is something that is considered a custom development. Be advised that this is beyond the scope of our support under our Support Policy. If you are unfamiliar with code and resolving potential conflicts, you may select our One service for further assistance.

Best Regards.

@ruenel

Thanks for the feedback, I actually have a partial working system with a filter I created for a custom looper.
{{dc:looper:field key="count"}} will output the total count for me.
{{dc:looper:field key="image_count_minus_3"}} will output the new image count.

I just can’t seem to get the images to pull in with {{dc:looper:field key="images.sizes.large"}}

Custom filter:

add_filter( 'cs_looper_custom_my_data', function( $data, $count, $image_count_minus_3 ) {
   $images = get_field( 'project_images' );
   $image_count = count( $images );

  // Calculate image_count minus 3
  $image_count_minus_3 = $image_count - 3;

  // Create an associative array with images and other data
  $data = [
    'images' => $images,
    'count' => $image_count,
    'image_count_minus_3' => $image_count_minus_3
  ];

  return $data;
}, 10, 3);

Sample debug_consumer

Index: 0
Current Data:
array(3) {
  ["images"]=>
  array(3) {
    [0]=>
    array(24) {
      ["ID"]=>
      int(87)
      ["id"]=>
      int(87)
      ["title"]=>
      string(16) "pb-image-test-01"
      ["filename"]=>
      string(20) "pb-image-test-01.jpg"
      ["filesize"]=>
      int(334263)
      ["url"]=>
      string(98) "https://wordpress.cloudwaysapps.com/wp-content/uploads/2023/08/pb-image-test-01.jpg"
      ["link"]=>
      string(68) "https://wordpress.cloudwaysapps.com/pb-image-test-01/"
      ["width"]=>
      int(1280)
      ["height"]=>
      int(720)
      ["sizes"]=>
      array(18) {
        ["thumbnail"]=>
        string(106) "https://wordpress.cloudwaysapps.com/wp-content/uploads/2023/08/pb-image-test-01-150x150.jpg"
        ["thumbnail-width"]=>
        int(150)
        ["thumbnail-height"]=>
        int(150)
        ["medium"]=>
        string(106) "https://wordpress.cloudwaysapps.com/wp-content/uploads/2023/08/pb-image-test-01-300x169.jpg"
        ["medium-width"]=>
        int(300)
        ["medium-height"]=>
        int(169)
        ["medium_large"]=>
        string(106) "https://wordpress.cloudwaysapps.com/wp-content/uploads/2023/08/pb-image-test-01-768x432.jpg"
        ["medium_large-width"]=>
        int(768)
        ["medium_large-height"]=>
        int(432)
        ["large"]=>
        string(107) "https://wordpress.cloudwaysapps.com/wp-content/uploads/2023/08/pb-image-test-01-1024x576.jpg"
        ["large-width"]=>
        int(1024)
        ["large-height"]=>
        int(576)
        ["1536x1536"]=>
        string(98) "https://wordpress.cloudwaysapps.com/wp-content/uploads/2023/08/pb-image-test-01.jpg"
        ["1536x1536-width"]=>
        int(1280)
        ["1536x1536-height"]=>
        int(720)
        ["2048x2048"]=>
        string(98) "https://wordpress.cloudwaysapps.com/wp-content/uploads/2023/08/pb-image-test-01.jpg"
        ["2048x2048-width"]=>
        int(1280)
        ["2048x2048-height"]=>
        int(720)
      }
    }
  }
  ["count"]=>
  int(3)
  ["image_count_minus_3"]=>
  int(0)
}

The documentation is lacking on the website for this detail.

Hi @designerken,

As you can’t perform any calculations on Dynamic Content, you can create a custom looper and return the exact result you are expecting. So, in this case, a custom looper is the correct solution.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.