Custom Looper params with dynamic content

Hi support

I’d like to add dynamic content to custom parameter into a Custom Looper.
something like that:

{
  "url" : "https://your-api/{{dc:post:id}}?_fields=id,date,link,title,excerpt"
}

What is the correct syntax for passing the parameter?

Thank You

Hi Daniele,

Thanks for reaching out.
The custom parameter can accept the dynamic content but it should be sent within the double quotes as a string. I have tested the following format which works perfectly.

{
    "parent_cat":"{{dc:post:id}}"
}

In that way, your format is correct. Please let us know if you find any issues with this.

Thanks

Dear tristup

But I’ve to combine dynamic content with the API url

I’ve to ADD Post ID in the middle of the API URL address

Thank You

Hey Daniele,

One way to do this is by adding post_id key in your JSON, for example I’ve the following JSON in my looper provider’s param’s field:

{
  "url" : "https://your-api/",
  "post_id": "425",
  "title": "Combined data"
}

For that I’ve the following code in my child theme’s functions.php

add_filter( 'cs_looper_custom_my_data', function( $result, $params ) {
  return $params;
}, 10, 2);

Now for instance If I have to use it in a Headline element, I can simply turn on the Link option and in the URL field I can combine these:

{{dc:looper:field key="url"}}{{dc:looper:field key="post_id"}}?_fields=id,date,link,title,excerpt

image

I hope this helps!

Thank you for your precious support

What I need is to send to my function.php a single string composed by three parts (composing the complete API URL):

  1. a static string: “https://your-api/” (first part of the API URL)
  2. a dynamic content: “{{dc:post:id}}” (second part of the API URL: containing the current Post ID)
  3. a static string: “?_fields=id,date,link,title,excerpt” (third part of the API URL)

I’ve to pass to my child theme’s functions.php the complete URL string in order to elaborate it and receive from it a JSON result with the API response, in order to consume that data in my Looper Consumer

Thanks

Hi Daniele,

There should not be any problem if you add the dynamic content within the parameter as mentioned by you. I have tested the same in local and found the output shown in the screenshot.

{
    "url":"https://your-api/{{dc:post:id}}?_fields=id,date,link,title,excerpt"
}

And you need to get the URL in your code like the example code given below.

add_filter('cs_looper_custom_apiurl', function($result, $args) {  
    $api_endpoint=$args['url'];
    echo $api_endpoint;
    //YOU CAN CALL the API ENDPOINT HERE
}, 10, 2);

test-new-Pro (3)

Now you can call URL as API endpoint, to get the output you are expecting. You can also use the way shown by my colleague in that case you need to construct the API endpoints with multiple parameters coming through the JSON.

add_filter('cs_looper_custom_apiurl', function($result, $args) {

    $api_endpoint=$args['url']."/".$args['post_id']."?_fields=".$args['title'];
    echo $api_endpoint;
    //YOU CAN CALL the API ENDPOINT HERE
}, 10, 2);

I would suggest you go through the following article and videos on the Looper.


Hope this helps.
Thanks

Dear trustup

Thank You for your support

After this:
{ "url":"https://your-api/{{dc:post:id}}?_fields=id,date,link,title,excerpt" }
and
add_filter('cs_looper_custom_apiurl', function($result, $args) { $api_endpoint=$args['url']; echo $api_endpoint; //YOU CAN CALL the API ENDPOINT HERE }, 10, 2);
as you suggested,
when I call the API with
$request = wp_remote_get( $api_endpoint );
I get strange behavior

if I use
{ "url":"https://your-api/1234?_fields=id,date,link,title,excerpt" }
(with static number) everything works fine

BUT if i use
{ "url":"https://your-api/{{dc:post:id}}?_fields=id,date,link,title,excerpt" }
(with dynamic ID) I receive an error “rest_no_route” error

Any idea?

Hi Daniele,

You might be doing something wrong in this, I would suggest you check by printing the $api_endpoint passing the {{dc:post:id}} in the parameter. If that prints the URL which you want to call, there might be something in your route which is the reason behind your issue. I have added the route and checked in a similar way and found it is working.

Please remember that investigating the custom codes is beyond the scope of our theme support, I would suggest you hire a developer who can assist you to do the customization or you can avail of our newly launched service called One, where the customization questions are answered.

Thanks

Dear tristup
I don’t want to take advantage of your patience

echo or print_r () displays the SAME content of $api_endpoint variable correctly

but the following STEP:
$request = wp_remote_get( $api_endpoint );

works fine only if
{ "url":"https://your-api/1234?_fields=id,date,link,title,excerpt" }
(with static ID number)

BUT if i use
{ "url":"https://your-api/{{dc:post:id}}?_fields=id,date,link,title,excerpt" }
(with dynamic ID) I receive an error “rest_no_route” error

Thank you

Hi Daniele,

Thanks for troubleshooting the steps, it seems that you are having some issue with your route, please check that the route is correctly defined and called.

Thanks

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