Getting Post Content in Custom Looper

I have a custom function that is being called as custom looper, to return the post content on a page.

I have a text field ready to output the text content using the following dynamic content tags: {{dc:looper:field key="testimonial_content"}}

The field is returned as JSON from the function as follows:
return [ [ "testimonial_content" => "$post_content" ] ];

Now the $post_content variable definitely contains the content, because I can see it when I either echo or var_dump(). So, with the following code at the end of my function, I should in theory get two copies of the content:
echo $post_content; return [ [ "testimonial_content" => "$post_content" ] ];

However, I only get the echoed content (which does show up on the site) and not the content being passed via the returned JSON. Is there a reason for this? I have done this before with other content from posts (such as getting the first image from a post when a featured image hasn’t been set) and all has worked fine previously, but this one has me stumped?!

Why is it working when I echo but not in JSON?

Sorry! I’m a complete clown! I hadn’t checked the ‘looper consumer’ button on one of the elements inside my nested looper! All working as expected! D’oh!

For anyone who’s interested (I’ve been meaning to post this for a while since seeing a post about apparently not being able to pull post content into dynamic content), this is how I’m pulling in post content to be used in dynamic content:

First, set up a looper provider for the post/custom post type you want to work with using the query builder looper type:

Then, within a child element of the looper consumer (you can add this to the same element as the provider if you want. I normally set up the provider on a row, and the consumers on columns) set up another looper of the custom type:
looper_provider_custom

I’m also going to pass within the parameters, the post ID of the current post within the parent loop as follows:

These parameters allow me to pull the content for that particular post within my function. The following function is placed within your theme’s (or child theme preferably) functions.php file:

add_filter( 'cs_looper_custom_testimonial_content', 'raise_testimonial_content', 10, 2 );

function raise_testimonial_content( $result, $params ) {
	$current_post = $params["current_item"];
	$current_post = filter_var($current_post, FILTER_SANITIZE_NUMBER_INT);
    $post = get_post( $current_post );
$post_content = apply_filters( 'the_content', $post->post_content );
return [
	[ "testimonial_content" => "$post_content" ]
];}

As you can see from above, the $post_content variable is being passed back inside the field testimonial_content which we can use in dynamic content as the key for a looper field.

Without forgetting to set a looper consumer for your nested custom looper (which I did earlier), you can then retrieve the post content that we returned within the field testimonial_content as follows:


Hope that helps someone (can’t remeber where the other post about this was originally)…

Hi @sguilliard,

Thanks for sharing your tips about custom loopers. Just for future topics, self responding or bumping your post pushes it back in our Queue system so it takes longer to respond to.

If you have any other concerns or clarifications regarding our theme features, feel free to open up a new thread.

Thank you.

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