0

I am trying to display entries based on the last segment in the url and then paginate.

Here's what I have so far but it's not displaying any entries:

{% set categorySlug = craft.request.getLastSegment() %}
  {% set category = craft.categories.slug(categorySlug).first() %}

  {% if category %}

      {% set entries = craft.entries.relatedTo(category).limit(12) %}
      <ul>

      {% paginate entries as entries %}

      {% for entry in entries %}

      {% for block in entry.resource.type('resource') %}

      <li class="summary m-all t-all d-all">
      <a href="{{ entry.url }}">
      <article>
      <header class="article-header">
      <h1>{{ block.resourceTitle }}</h1>
      <h5>{{ block.resourceAuthors }}</h5>
      <p>{{ block.resourceSummary }}
      <span class="link">Read more</span>
      </p>
      </header>
      </article>
      </a>
      </li>
      <hr class="m-all t-all d-all">

      {% endfor %}

      {% endfor %}

      </ul>

      {% if paginate.prevUrl %}
          <a href="{{ paginate.prevUrl }}">Previous</a>
      {% endif %}

      {% if paginate.nextUrl %}
          <a href="{{ paginate.nextUrl }}">Next</a>
      {% endif %}
      {% endpaginate %}

  {% endif %}

Even this simplified version outputs zero entries:

  {% set categorySlug = craft.request.getLastSegment() %}
  {% set category = craft.categories.slug(categorySlug).first() %}

  {% set entries = craft.entries.relatedTo(category) %}

  {# Output them #}
  {% for entry in entries %}
      <a href="{{ entry.url }}">{{ entry.title }}</a>
  {% endfor %}

Any thoughts?

  • Your simplified version, which is generic enough for me to test without modification, works fine for me. So, I'm wondering if the problem doesn't lie elsewhere than in the code snippet. – Jack McKenzie May 27 '15 at 20:07
1

I'm rolling the same thing with

{% extends 'layouts/_entry_layout' %}
{# sets the name for the css file to be pulled #}
{% set pageType = 'search' %}
{% set catSlug = craft.request.getSegment(2) %}
{% set title = craft.categories.slug(catSlug).first() ~ ' Channel' %}

{% block content %}

<section class="search-results">

{% paginate craft.entries.relatedTo(category) as entriesOnPage %}


<h1 class="category__title">{{ category.title }}</h1>

    {% for entry in entriesOnPage %}
        {% include 'modules/_article_listing' %}
    {% endfor %}

{% include 'inc/_pagination' %}

{% endpaginate %}

</section>

{% endblock %}

my article listing module contains the usual things like {{ entry.title }} etc

and pagination include is like so:

{% if paginate %}

{% set currentPage = paginate.currentPage %}
{% set startPage = currentPage - 2 %}
{% set endPage = currentPage + 2 %}
{% if query is defined %}
{% set queryString = "?q=" ~ query %}
{% endif %}


{% if paginate.totalPages > 1 %}
<nav class="pagination" role="navigation">

    <ol class="pagination__list">

        {% if paginate.prevUrl %}
        <li class="pagination__item">
            <a class="btn  btn-2  btn-2a" href="{{ paginate.prevUrl }}{% if queryString is defined %}{{ queryString }}{% endif %}">
                &larr;
                <span class="visuallyhidden">Previous Page</span>
            </a>
        </li>
        {% endif %}
        {% for i in startPage..endPage %}
        {% if paginate.getPageUrl( i ) %}
        {% if i != currentPage %}

        <li class="pagination__item">
            <a class="btn  btn-2  btn-2a" href="{{ paginate.getPageUrl( i ) }}{% if queryString is defined %}{{ queryString }}{% endif %}">
                <span class="visuallyhidden">Page </span>{{ i }}
            </a>
        </li>

        {% else %}

        <li class="pagination__item btn  -current">
            <span class="visuallyhidden">Current Page </span>{{ i }}
        </li>

        {% endif %}
    </li>
    {% endif %}
    {% endfor %}

    {% if paginate.nextUrl %}
    <li class="pagination__item">
        <a class="btn  btn-2  btn-2a" href="{{ paginate.nextUrl }}{% if queryString is defined %}{{ queryString }}{% endif %}">
            <span class="visuallyhidden">Next Page</span>
            &rarr;
        </a>
    </li>
    {% endif %}

</ol>



</nav>
{% endif %}

{% endif %}
0

My category field was inside a Matrix field.

These links helped with the twig logic:

Is it possible to have category listing page of entries if the category is defined within a matrix block inside said entries?

Find entries that have a category selected from a category field within a matrix block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.