ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Display Category Archive for a Subset of a Channel's Entries

April 14, 2012 9:59am

Subscribe [2]
  • #1 / Apr 14, 2012 9:59am

    boldyellow

    89 posts

    I’ve spent alot of time (alot) in the EE forums and documentation, but my skills may not be advanced enough to fully understand how this issue has been solved.  So I’m kindly seeking some input from the community for clarification and to point me in the right direction(s).


    I have a newsletter with articles that are assigned by issue date (Dec11, Jan12, Feb12, etc.).  And each article is assigned to a single parent or child category (fyi, categories are frequently added or edited).


    The category_archive tag works perfectly to display all the channel entries in their proper categories, like this…

    Hospitals
    • Bon Secours
      —article:  New CEO Interview (Jan 2012)
      —artlcle:  Labor Management News (Feb 2012)
    • Sinai
      —article:  New Expansion Announced (Feb 2012)
    • Mercy
      —article:  Community Initiatives (Jan 2012)
    Insurance
      —article:  Reimbursement Coding Issues (Jan 2012)


    But I’d like to display the category archive for a subset of entries—such as the Jan 2012 articles, like this…

    Hospitals
    • Bon Secours
      —article:  New CEO Interview (Jan 2012)
    • Mercy
      —article:  Community Initiatives (Jan 2012)
    Insurance
      —article:  Reimbursement Coding Issues (Jan 2012)


    I’m unclear if any add-ons can actually do this (like category sorted entries?)  Or if I ran an exp query to select Jan 2012 articles, could I display the category archive from that result?


    Any guidance would be appreciated… thanks!

  • #2 / Apr 15, 2012 10:23pm

    Man With A Peg

    124 posts

    Hey BoldYellow,

    You might have to sprinkle some PHP into this solution. If I were in your shoes, I would use the year= and month= parameters in the {exp:channel:entries} tag, then create a PHP array to order the posts by category.

    Something like this:

    <?php $entries = array(); ?>
    {exp:channel:entries year="2011" month="12"}
        {categories}<?php $entries["{category_name}"]["{entry_id}"] = "{title}"; ?>{/categories}
    {/exp:channel:entries}
    
    <?php
    ksort($entries);
    foreach ($entries as $category => $articles) {
        echo '<h3>'.htmlentities($category).'</h3><p>';<br />
        foreach ($articles as $entry_id => $title) {<br />
            echo '<a href="/path/to/entry/.$entry_id.">'.htmlentities($title).'</a><br />
    ';<br />
        }<br />
    }<br />
    ?>

    As long as none of the category names or article titles has double quotes in it, this should work great. If these articles are being written with SafeCracker’s help, you may need to move this type of code into a plugin.

  • #3 / Apr 16, 2012 10:28am

    boldyellow

    89 posts

    Thanks EpicVoyage… that definitely gets me much closer to where I was.  So, now I’m looking at the parent and child category structure.

    The entries are assigned into child categories.  In turn, most of the child categories are assigned to a parent category… is it a matter of running an extra foreach loop to get the parent category names to display?

    Parent 1
    • Child 1
      —article: facts
      —article: news
    • Child 2
      —article: profile
    Parent 2
      —article: strategy
    etc…

  • #4 / Apr 16, 2012 10:42am

    Man With A Peg

    124 posts

    Yes, one extra foreach loop and an extra dimension on the variable (using the square brackets: []) to contain the parent category. Do you need me to write this out for you?

  • #5 / Apr 16, 2012 10:56am

    boldyellow

    89 posts

    Well, I was working on it for a while doing some tests but was getting hung up on how to get the parent category names to come in. 

    My PHP has never been stellar and since my brain cells are less nimble than they used to be…  yes, if you have some code I can play with, I’d be very much appreciative.

  • #6 / Apr 18, 2012 4:10pm

    Man With A Peg

    124 posts

    Hey BoldYellow,

    I hope that you will forgive me for taking a couple days to get back to this. I have not dealt much with categories in the last few months, so I wanted to test my code to make sure it worked (the initial code was off the top of my head). This version needed to be tested.

    First, create another template. This is necessary due to a conflict in the {category_name} variable between the {exp:channel:categories} and {categories} tags/tag pairs. I placed mine at includes/.category_group. If you use a different location, be sure to update the second template. This will be a one-liner:

    {exp:channel:categories show='{embed:cat}' style='linear'}{category_name}{/exp:channel:categories}

    Next, go back to the template I gave you earlier. Here is the new code:

    <?php
    $entries = array();
    {exp:channel:entries channel="test" year="2012" month="4"}
    {categories}$entries["{embed='includes/.category_group' cat='{parent_id}'}"]["{category_name}"]["{entry_id}"] = "{title}";{/categories}
    {/exp:channel:entries}
    
    ksort($entries);
    foreach ($entries as $parent => $categories) {
     if (!empty($parent)) {
      echo '<h2>'.htmlentities($parent).'</h2><p>';<br />
     }</p>
    
    <p> ksort($categories);<br />
     foreach ($categories as $category => $articles) {<br />
      echo '</p><h3>'.htmlentities($category).'</h3><p>';<br />
      foreach ($articles as $entry_id => $title) {<br />
       echo '<a href="/path/to/entry/.$entry_id.">'.htmlentities($title).'</a><br />
    ';<br />
      }<br />
     }<br />
    }<br />
    ?>

    PHP should be enabled on output. It occurred to me while testing that opening and closing multiple PHP tags was pointless, due to the parse order of the template, so I cleaned that up as well. Any categories without a parent category are going to be displayed first.

    ksort() is used to sort the parent and child categories by name. It is a standard PHP function that sorts an array by key (instead of value). That is where we are storing the categories. Imagine this as the pseudo-code:

    $entries[ {category_parent} ][ {category_child} ][ {entry_id} ] = {entry_title}

    Then we use nested foreach() loops to go through the array and display the organized data.

    Hope this helps!

  • #7 / Apr 19, 2012 5:06pm

    boldyellow

    89 posts

    Wow, that’s awesome… I’ll test and tinker with this and let you know how I get along.  Thanks very much for the help!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases