Jump to content
alejandro

New Options Fieldtype

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.

Share this post


Link to post
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

Share this post


Link to post
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  :) .

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...