I need your help putting together a little piece of PHP. Please help!
For a custom post type called "Bags"...
I created a repeatable custom field group called "Product Slider Item"...
Which consists of two fields...
... an image field called "Product Slider Image"
... a WYSIWYG field called "Product Slider Image Caption"
(all slugs the names above, lower case with dashes between the words.)
Here's a screenshot: hidden link
I am trying to create a piece of PHP code that will let me display the images and text in the custom fields. I need to display each image with the caption, and then the next image with its caption... as many times as there are "Product Slider Items."
I have read this page (several times) https://toolset.com/documentation/customizing-sites-using-php/displaying-repeatable-groups-of-fields/
I cannot get anything to work... I don't know what slugs to use and how to call these custom fields out.
Can you please help me?? I just paid $150 for this plugin and I really need it to work 😐
thanks for any help you can give me.
I cannot get anything to work... I don't know what slugs to use and how to call these custom fields out.
It depends on where this code is being used and several other variables, like the options you want to use for each custom field. I'm going to assume that you have access to the current "Bag" post ID in a variable $bag_id, and the slugs you mentioned are accurate. From there, you can do something like this:
$rfgs = toolset_get_related_posts(
$bag_id,
'product-slider-item',
array(
'query_by_role' => 'parent',
'return' => 'post_id'
)
);
Now the $rfgs variable should hold an array of post IDs that correspond to each repeatable field group (RFG) associated with the bag post with ID $bag_id. If you want to confirm that, var_dump the $rfgs variable or log it with error_log:
error_log(print_r( $rfgs, true));
If no related RFGs are found, then there is a problem with the slugs or some other issue we would need to take a closer look at. If the array holds a list of numbers, then you can loop over those $rfgs IDs and render the image and caption values using the types_render_field function. Something like this:
foreach( $rfgs as $rfg ) {
echo types_render_field( "product-slider-image", array( "width" => "300", "height" => "200", "proportional" => "true", "id" => $rfg ) );
echo "<br />";
echo types_render_field( "product-slider-caption", array( "output" => "normal", "id" => $rfg ) );
echo "<br />";
}
Obviously this is a very simple case. The types_render_field function is documented for each field type here: https://toolset.com/documentation/customizing-sites-using-php/functions/#textfield
Click the orange "More info" buttons to see examples.
Christian — I'm trying to get some of this code to work... Here are the errors I'm getting.
I use this code here:
$rfgs = toolset_get_related_posts(
$bag_id,
'product-slider-item',
array(
'query_by_role' => 'parent',
'return' => 'post_id'
),
);
And get this error:
PHP Parse error: syntax error, unexpected ')' in /Users/chasereeves/Dropbox/Clients/htdocs/bagworks/wp-content/themes/bagworks02/parts/template-productcarousel.php on line 20
On line 20 is the closing ')' at the end of the code block above.
I'm hoping you can help me get this first part working and then I'll be able to echo out the display of the images and text myself.
That was a mistake in my code, sorry. Please delete the comma before the last line to resolve that error. I will update my snippet above.