Jump to content

Categories for post limited by parent page


Re4DeR
 Share

Recommended Posts

Hello, I have structure of site like in picture. I  use blog module to have basic post and categories structure. So I have field "categories" for posts. And this field is limited by template "category" . By default I get categories from site1 and site2.  I cant use parent limitation coz I would like to have same field in site1 and site2.  

So I think I need to get root parent for current post and use categories with same parent.  I think I am not able to use admin ui and I have to create custom php function? Or is it any other way? Or maybe I am completly wrong and there is some other, better way - I am using proccesswire first time but I am very satisfied with this cms. 

 

Thanks for any reply.

processwire.png

Link to comment
Share on other sites

Welcome to ProcessWire and the forums @Re4DeR.

Something like below added in /site/ready.php should do the trick. If you don't have a ready.php file, just create one and namespace it to ProcessWire;

 $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    if($event->object->hasField == 'categories') {
      $sitePage = $event->arguments('page')->rootParent;
      $siteCategories = $sitePage->child('template=categories')->children;
      if($siteCategories->count()) {
          $event->return = $siteCategories;
      }
    }
  });

 

With the above code, individual posts in Site 1 will only be able to select categories from Site 1 and for Site 2, they'll only be able to select from their site.

Please note:

  1. The page (reference) field is called categories
  2. The template of the 2 Categories pages (i.e. the parents of your individual categories) is called categories.
  3. In the page field categories, we don't input anything in the Selectable pages section.

Give us a shout if you have any questions.

Edited by kongondo
  • Like 3
Link to comment
Share on other sites

2 hours ago, Re4DeR said:

So I think I need to get root parent for current post and use categories with same parent.

Yes, you were on the right track, although there could be other ways of doing it as well depending on the site's setup.

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...