Jump to content

New Options Fieldtype


alejandro
 Share

Recommended Posts

Hello,

I was thinking to use this new field (from dev branch 2.5.17) to create several categories and then select one or multiple categories in each page. But I need to ouput the all the categories available in order to generate a navigation menu.

I created the field "categories" with several "selectable options" then tried:

$cat = $fields->get("categories");
foreach($cat as $c) {
 $c->title;
}

But this doesn´t output anything.

Is there any other way?

Thanks in advance, Alejandro.

Link to comment
Share on other sites

Categories are really a perfect use case for pages rather than options. But to answer your question, this should do it:

$options = $fieldtypes->get('FieldtypeOptions')->getOptions('categories'); 
foreach($options as $option) {
  echo $option->title; 
}

This also works:

$field = $fields->get('categories');
$options = $field->type->getOptions($field); 

If you are going to be using the selections for anything, use the $option->id property, rather than the title, just in case you later go back and change the title or something. 

  • Like 3
Link to comment
Share on other sites

Categories are really a perfect use case for pages rather than options.

Mmm, it just seemed more simple, no need to think in a template, a parent page to hold the categories... not that is to much work in any case. Maybe I'd use the pages approach.

Thank you anyway for the useful info  :) .

Link to comment
Share on other sites

With pages you have "category" pages already :)

Otherwise you need to build category views using url segments, get variables or so...

Of course if you use categories only for presentation, then options might be faster / easier.

  • Like 2
Link to comment
Share on other sites

Of course if you use categories only for presentation, then options might be faster / easier.

Yes, that's the case, I'm building a filterable portfolio, so I need the categories only as content "selectors/filters". But maybe in the future I could need those category pages and then I would need to create them anyway so it seems a more versatile solution.

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...