Currently it's not possible to define your own show_on conditions. I've been trying to get some fields to only show on the post edit screen of the front page but this is impossible right now without hacks. There are no filters or anything attached to the show_on logic in the post meta container. I quite like how CMB2 handles this. They allow you to define a show on callback. See this.
Thanks for the feedback!
You can currently do that: ->show_on_page( (int) get_option( 'page_on_front' ) )
You're right, however, that this should be easier / more intuitive. We're going to improve the documentation and consider API changes.
That's actually a really nice and simple way to do it. Thank you! I can't believe I didn't think about that yesterday.
Sounds great! The ability to define your own conditions would be really cool :)
Useful feature would also be to allow show_on_page to accept an array of page IDs to show on.
Another useful method will be show_on_post that can work for all post types.
This relates to: https://github.com/htmlburger/carbon-fields/issues/183
Hi,
There seems to be some quirks in either how the show_on_* methods work, or they might not be documented clearly enough.
I was trying to add a container to a template and a specific page. But that seems to be impossible with the current setup. In the docs it is not mentioned if adding more show_on_* methods work to include more posts or exclude based on matching all methods.
For example:
Should this add the CF to any page with the template_name.php template __and__ the some_page_id page? Or should it only apply to some_page_id if it has template_name.php set as template?
Container::make('post_meta', 'some_name')
->show_on_template('templates/template_name.php')
->show_on_page('some_page_id');
Right now it seems like a lot of what i would suspect are quite normal use cases are very hard to achieve.
For example, how would you display a a container on a page and it's child pages?
This does not work:
Container::make('post_meta', 'Container name')
->show_on_page('parentName')
->show_on_page_children('parentName')
However stacking show_on_page method calls does show the container on several pages:
My bad, this also does not insert the container on two pages:
Container::make('post_meta', 'Container name')
->show_on_page('some_page_id')
->show_on_page('some_other_page_id')
Hi @Jeger ,
The conditionals are somewhat limited right now and are in need of a rework so I understand your frustration.
Better conditionals and validation filters are high on our priority list for 2.0 right now so we should have an improved system soon.
Currently, conditionals can only be called once (subsequent calls override previous ones) and different conditionals have an "AND" relation between each other.
Container::make('post_meta', 'some_name')
->show_on_template('templates/template_name.php')
->show_on_page('some_page_id');
This will show the container only on the some_page_id page IF it has the template_name.php template assigned to it.
Container::make('post_meta', 'Container name')
->show_on_page('parentName')
->show_on_page_children('parentName')
This requires that a page is both parentName and a child of parentName so it will never show up. To show the container on both, for CF 1.5 you'll have to go in a bit of a round-about way like this:
function get_my_fields() {
return array(
Field::make(),
// ...
);
}
Container::make('post_meta', 'Container name')
->show_on_page( $parent_id )
->add_fields( get_my_fields() );
Container::make('post_meta', 'Container name')
->show_on_page_children( $parent_id )
->add_fields( get_my_fields() );
_Note: never pass the same field(s) by reference to multiple containers - always supply containers with fresh instances (which is why a function is used here instead of an array of field references)._
Container::make('post_meta', 'Container name')
->show_on_page('some_page_id')
->show_on_page('some_other_page_id')
Identical conditionals are overridden by the latter so this should show the container only on the second page.
@atanas-angelov-dev dude, thank you so much :)
Awesome clarifications!
TBH, i don't think having a function that creates those fields is a bad interim solution at all, I was getting stuck on, like you said, passing it by reference.
2.0.0 has been released which allows you to achieve this using the new conditionals system:
More info here: https://carbonfields.net/docs/containers-conditional-display/?crb_version=2-0-0
Most helpful comment
Useful feature would also be to allow
show_on_pageto accept an array of page IDs to show on.