Hi, I am integrating prebuilt layouts like sliders, parallax etc in a builder made with custom fields (i use carbon fields with complex field that does the same as ACF flexible content).
In the beginning i was putting a custom field for classes but finally i find it a little messy.
So i have decided to write the html with the beans functions so i can modificate the attributes with the markup id.
The problem is that my layouts are reusable, so if for exemple i use 2 times the slider but i want to configure it differently (for ex having a different animation), the 2 sliders will have the same markupid, so my modification will affect both.
So i imagine something like it is done with the beans widgets, if i put 2 text widgets the second will have [_text-2] at the end of the markupid ...
I want to do the same but i don't know how...
Hey Alex,
Beans sub-filters are perfect for that (read documentation here). So if you are using beans_open_markup()
and beans_close_markup()
, you can add sub-filters in between square brakets. Here is an example:
$unique_id = 'unique';
beans_open_markup( "example_item[_{$unique_id}]", 'div', array( 'class' => 'example-class' ) );
// Some stuff.
beans_close_markup( "example_item[_{$unique_id}]", 'div' );
Then users will be able to add a class to all the example markup as such:
beans_add_attribute( 'example_item', 'class', 'another-class' );
Or target a specific item which would have an id unique
for example as follow:
beans_add_attribute( 'example_item[_unique]', 'class', 'another-class' );
This gives users great flexibility and the ability to precisely target markups π
Hope that helps!
Yes, i have understood that, my question was how to set dynamically this unique ID, incrementing it on each new layout.
So it was a pure php question, i already find the solution, so now each new layout i used as builder_text_editor[_numberx] , the number increment each time a layout is used.
It's super cool!
Actually i thought i add the solution, but it still have a problem.
i create this function :
function counter( ) {
static $count = 0;
$count++ ;
return $count;
}
Then i do that
$number = counter();
echo beans_open_markup('builder_text_editor[_'.$number.']','div',array('class'=>'uk-container uk-container-center uk-margin-large'));
It works if the elements are on the same page, but if elements are on different pages, then the counter is reset... i use this in a template page, perhaps it's for that...
I could use a custom field to set the unique id, but i really would like to have it setup dynamically
Hey Alex,
I doubt you need to build a counter, I am pretty sure that Carbon Fields->Complex Fields are using unique ids or integer.s Since they are re-usable, they have to be registered them in a unique way. Use what ever Carbon Field uses to make Complex Fileds unique to set your Beans Markup. It goes hand in hand π
Happy coding,
Hi, This is clever but i don't see any id or integer in the ccomplex field, perhaps i will ask to them, they are very supportive. Anyway for the moment i have solved it putting a custom field, and perhaps it's the best solution as being able to put a true word for a filter is better than a number, and with that i can also setup multiple filters. The more i dig in beans , the more i discover things π
Hey Alex,
What I mean is that when you retrieve the complex fields using carbon_get_post_meta()
, it returns an array of key values since it can be used multiple times. You could use the array keys to set your sub-filters. That said it is not something I would really advise as it may lead to use quite a bit of PHP memory and since the array items will depend on the user entries, the sub-filters will be subject to change which isn't a good idea! I can't think of a better solution since you are trying to add sub-filters for dynamic values though.
Hope that helps,
Thank you Thierry,
I will still with the custom fields !