Answers.Body: <p>Ok so I figured out the issue. I had the category field setup to have a limit of 1, which I assumed would be ok. My thinking was that you're only choosing 1 state. But because state is a sub category of country I need to allow for country and state AKA a limit of 2. Once I did this I was able to structure my template accordingly:</p>
  
  <pre><code>{% if user.addressMatrix | length %}
  
      {% for addressInfo in user.addressMatrix %}
  
           {% if addressInfo.type == "address" %}
  
              {% for item in addressInfo.region %}
  
                  {% if item.level == 2 %}
  
                  {{ item.title }}
  
                  {% endif %}
  
              {% endfor %}
  
          {% endif %}
  
     {% endfor %}
  
  {% endif %}
  </code></pre>
  
  <p>I think that future releases of Craft ought to have more options for category fields, like: Restricting selections to a sub category, limiting the number of sub categories. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-28T14:37:03.893Z
Answers.LastActivityDate: 2015-04-28T14:37:03.893Z
Answers.UserDisplayName: Matt V
Body: <p>I have my users setup with a category field that links to a category of countries and each country has states/provinces.</p>
  
  <p>I have my form setup to only display the states listed under United States. The plugin I created saves this data as expected.</p>
  
  <p>The problem I'm having is accessing the state which was saved for the user. This is my current code. It works BUT there has to be a better way:</p>
  
  <pre><code>{% if user.addressMatrix | length %}
  
      {% for addressInfo in user.addressMatrix %}
  
          {% set cat = addressInfo.region[1] %}
  
          {{ cat.title }}
  
     {% endfor %}
  
  {% endif %}
  </code></pre>
  
  <p>Please advise and thanks in advance.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-27T17:24:18.790Z
Comments.Text: The reason this is complex is because you are storing the address info in a matrix field. If you only need one address you might consider creating individual fields (i.e. address, city, region, postCode, etc)—you could then access the region using {{ user.region[0].title }}. Otherwise, I think your solution is the way to do it (although you can remove the 'if' conditional; if the matrix is empty then the 'for' loop won't execute anyway).
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-27T17:49:54.750Z
Comments.Text: I don't see what a matrix field has to do with anything. My category field is a regular old category field with the matrix. Accessing the data should be the same process as if it were standalone.
Comments.UserDisplayName: Matt V
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-27T17:59:17.850Z
Comments.Text: I'm assuming that 'addressMatrix' is a matrix field? With a category field called 'region' defined in one of the blockTypes?
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-04-27T18:13:49.300Z
Comments.Text: Correct. I just now realized I didn't list the block in the above code. I was trying to provide a simplified version of the template for readability sake. Yes 'region' is a category field in a block, in a matrix. What I don't like about how I'm outputting the title is I have no idea if the thing I'm outputting is a child, parent, child of a child, etc. There is no context.
Comments.UserDisplayName: Matt V
Tags: <templating><categories>
Title: Get saved sub category on user
ThreadText: # Get saved sub category on user
  
  (Asked by: username_0 on 2015-04-27)
  
  I have my users setup with a category field that links to a category of countries and each country has states/provinces.
  
  I have my form setup to only display the states listed under United States. The plugin I created saves this data as expected.
  
  The problem I'm having is accessing the state which was saved for the user. This is my current code. It works BUT there has to be a better way:
  
  ```
  {% if user.addressMatrix | length %}
  
      {% for addressInfo in user.addressMatrix %}
  
          {% set cat = addressInfo.region[1] %}
  
          {{ cat.title }}
  
     {% endfor %}
  
  {% endif %}
  
  ```
  
  Please advise and thanks in advance.
  
  **Tags:** templating, categories
  
  > **Comments:**
  > **username_1**: The reason this is complex is because you are storing the address info in a matrix field. If you only need one address you might consider creating individual fields (i.e. address, city, region, postCode, etc)—you could then access the region using {{ user.region[0].title }}. Otherwise, I think your solution is the way to do it (although you can remove the 'if' conditional; if the matrix is empty then the 'for' loop won't execute anyway).  
  > **username_0**: I don't see what a matrix field has to do with anything. My category field is a regular old category field with the matrix. Accessing the data should be the same process as if it were standalone.  
  > **username_1**: I'm assuming that 'addressMatrix' is a matrix field? With a category field called 'region' defined in one of the blockTypes?  
  > **username_0**: Correct. I just now realized I didn't list the block in the above code. I was trying to provide a simplified version of the template for readability sake. Yes 'region' is a category field in a block, in a matrix. What I don't like about how I'm outputting the title is I have no idea if the thing I'm outputting is a child, parent, child of a child, etc. There is no context.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-04-28. Score: 1
  
  Ok so I figured out the issue. I had the category field setup to have a limit of 1, which I assumed would be ok. My thinking was that you're only choosing 1 state. But because state is a sub category of country I need to allow for country and state AKA a limit of 2. Once I did this I was able to structure my template accordingly:
  
  ```
  {% if user.addressMatrix | length %}
  
      {% for addressInfo in user.addressMatrix %}
  
           {% if addressInfo.type == "address" %}
  
              {% for item in addressInfo.region %}
  
                  {% if item.level == 2 %}
  
                  {{ item.title }}
  
                  {% endif %}
  
              {% endfor %}
  
          {% endif %}
  
     {% endfor %}
  
  {% endif %}
  
  ```
  
  I think that future releases of Craft ought to have more options for category fields, like: Restricting selections to a sub category, limiting the number of sub categories.
  
  
  ---
  
  