Answers.Body: <p>First you need to define the category. If your category group has the “Categories in this group have their own URLs” checkbox checked, and the URL Format is set to <code>category/{slug}</code>, and this is happening on the template you’ve specified in the Category Template setting, then all the stars have aligned and this step is taken care of for you automatically. The category will already be available to your template, and it will be called <code>category</code>.</p>
  
  <p>Otherwise you will have to fetch it yourself:</p>
  
  <pre><code>{% set category = craft.categories({
      group: 'workCategories',
      slug: craft.request.getSegment(2)
  }).first() %}
  
  {% if not category %}
      {% exit 404 %}
  {% endif %}
  </code></pre>
  
  <p>Once you have your category, you can fetch the related Clients using the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param">relatedTo</a> param on a craft.entries query:</p>
  
  <pre><code>{% set clients = craft.entries({
      section: 'clients',
      relatedTo: {
          targetElement: category,
          field: 'workSamples.myCategoriesField'
      }
  }) %}
  </code></pre>
  
  <p>That’s saying: “Give me the entries in the <code>clients</code> channel, which are related to <code>category</code> (where <code>category</code> is the <strong>target</strong> of the relation), via the <code>myCategoriesField</code> sub-field of a <code>workSamples</code> Matrix field.” Make sense?</p>
  
  <p>Once you have those client entries you can loop through them, and show their respective Matrix blocks which are related to the category:</p>
  
  <pre><code>{% for client in clients %}
      &lt;h3&gt;{{ client.title }}&lt;/h3&gt;
  
      {% for workSample in client.workSamples.relatedTo({ targetElement: category }) %}
          {% for image in workSample.myAssetsField %}
              &lt;img src="{{ image.url }}"&gt;
          {% endfor %}
  
          {% for row in workSample.myTableField %}
              {{ row.youtubeId }}
          {% endfor %}
      {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>If there are going to be a lot of clients/Matrix blocks/assets getting looped through, it’s probably going to be a good idea to wrap the whole thing in <code>{% cache %}</code>...<code>{% endcache %}</code> tags, as there’s potential for the query count to really pile up here.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-21T07:33:05.877Z
Answers.Text: Thank you kind sir! Your explanation helped me out a ton on this (compared to the docs). I think you just gave me that "ah-ha" moment in regards to relations.
Answers.UserDisplayName: Paulo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-19T21:44:29.510Z
Answers.LastActivityDate: 2014-11-19T21:53:09.393Z
Answers.LastEditDate: 2014-11-19T21:53:09.393Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I think I have been staring at this problems too long and I need a kick in the right direction...</p>
  
  <p>Here is what I have (the content modeling is a tad wonky but this is what we're dealing with):</p>
  
  <p>Clients Channel</p>
  
  <ul>
  <li>Work Samples Matrix Field (1 matrix block inside the matrix field)
  <ul>
  <li>Category matrix block field pulling from work categories</li>
  <li>Assets matrix block field to upload images</li>
  <li>Table matrix block field to enter youtube ID's</li>
  </ul></li>
  </ul>
  
  <p>This is what I need to do:</p>
  
  <p>When a category is selected by a user I take the user to a category listing page that contains all clients (from the Clients channel) with work samples within that category (categories within the matrix block from each client in the client channel).</p>
  
  <p>For example: All clients with "website" samples should be displayed on a page at <a href="http://domain.tld/category/website" rel="nofollow">http://domain.tld/category/website</a></p>
  
  <p>What is the most efficient way to pull this off in Craft/twig?</p>
  
  <p>Reading the Craft docs is getting me about 1/2 there but something in the relationships docs isn't clicking yet.</p>
  
Tags: <matrix><categories>
Title: Is it possible to have category listing page of entries if the category is defined within a matrix block inside said entries?
ThreadText: # Is it possible to have category listing page of entries if the category is defined within a matrix block inside said entries?
  
  (Asked by: username_0 on 2014-11-19)
  
  I think I have been staring at this problems too long and I need a kick in the right direction...
  
  Here is what I have (the content modeling is a tad wonky but this is what we're dealing with):
  
  Clients Channel
  
  * Work Samples Matrix Field (1 matrix block inside the matrix field)
    + Category matrix block field pulling from work categories
    + Assets matrix block field to upload images
    + Table matrix block field to enter youtube ID's
  
  This is what I need to do:
  
  When a category is selected by a user I take the user to a category listing page that contains all clients (from the Clients channel) with work samples within that category (categories within the matrix block from each client in the client channel).
  
  For example: All clients with "website" samples should be displayed on a page at <http://domain.tld/category/website>
  
  What is the most efficient way to pull this off in Craft/twig?
  
  Reading the Craft docs is getting me about 1/2 there but something in the relationships docs isn't clicking yet.
  
  **Tags:** matrix, categories
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-11-19. Score: 9 | Accepted answer
  
  First you need to define the category. If your category group has the “Categories in this group have their own URLs” checkbox checked, and the URL Format is set to `category/{slug}`, and this is happening on the template you’ve specified in the Category Template setting, then all the stars have aligned and this step is taken care of for you automatically. The category will already be available to your template, and it will be called `category`.
  
  Otherwise you will have to fetch it yourself:
  
  ```
  {% set category = craft.categories({
      group: 'workCategories',
      slug: craft.request.getSegment(2)
  }).first() %}
  
  {% if not category %}
      {% exit 404 %}
  {% endif %}
  
  ```
  
  Once you have your category, you can fetch the related Clients using the [relatedTo](http://buildwithcraft.com/docs/relations#the-relatedTo-param) param on a craft.entries query:
  
  ```
  {% set clients = craft.entries({
      section: 'clients',
      relatedTo: {
          targetElement: category,
          field: 'workSamples.myCategoriesField'
      }
  }) %}
  
  ```
  
  That’s saying: “Give me the entries in the `clients` channel, which are related to `category` (where `category` is the **target** of the relation), via the `myCategoriesField` sub-field of a `workSamples` Matrix field.” Make sense?
  
  Once you have those client entries you can loop through them, and show their respective Matrix blocks which are related to the category:
  
  ```
  {% for client in clients %}
      <h3>{{ client.title }}</h3>
  
      {% for workSample in client.workSamples.relatedTo({ targetElement: category }) %}
          {% for image in workSample.myAssetsField %}
              <img src="{{ image.url }}">
          {% endfor %}
  
          {% for row in workSample.myTableField %}
              {{ row.youtubeId }}
          {% endfor %}
      {% endfor %}
  {% endfor %}
  
  ```
  
  If there are going to be a lot of clients/Matrix blocks/assets getting looped through, it’s probably going to be a good idea to wrap the whole thing in `{% cache %}`...`{% endcache %}` tags, as there’s potential for the query count to really pile up here.
  
  > **Comments:**
  > **username_0**: Thank you kind sir! Your explanation helped me out a ton on this (compared to the docs). I think you just gave me that "ah-ha" moment in regards to relations.  
  
  ---
  
  