ee-garage.com

Linear list of categories with entries

Renders a linear list of categories with all entries assigned to the category in the {category_channel_entries}.

<ul>
{exp:nsm_categories:archive
  enable="entries"
}
  <li class="{switch='even|odd'}">
    category_name: <strong>{category_name}</strong>
    <ul>
      <li>count: <strong>{count}</strong></li>
      <li>category_id: <strong>{category_id}</strong></li>
      <li>category_group_id: <strong>{category_group_id}</strong></li>
      <li>category_parent_id: <strong>{category_parent_id}</strong></li>
      <li>category_url_title: <strong>{category_url_title}</strong></li>
      <li>category_description: <strong>{category_description}</strong></li>
      <li>category_image: <strong>{category_image}</strong></li>
      <li>category_order: <strong>{category_order}</strong></li>
      <li>category_channel_entry_total_results: <strong>{category_channel_entry_total_results}</strong></li>
      <li>category_channel_entries:
        <ul>
          {if "{category_channel_entry_total_results}" == "0"}
          <li>No results</li>
          {/if}
          {category_channel_entries}
          <li class="{switch='one|two|three'}">
            <ul>
              <li>entry_title: <strong>{title}</strong></li>
              <li>entry_id: <strong>{entry_id}</strong></li>
              <li>entry_url_title: <strong>{url_title}</strong></li>
              <li>entry_count: <strong>{entry_count}</strong></li>
            </ul>
          </li>
          {/category_channel_entries}
        </ul>
      </li>
    </ul>
  </li>
{/exp:nsm_categories:archive}
</ul>

Tabbed interface

Creates a tabbed interface of categories ready for jQuery UI.

<!-- Please note that this example can be improved with clever use of NSM Transplant or Stash -->

<!-- This code has been included from the jQuery UI website to quickly demonstrate a jQuery UI tabbed interface -->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<script>
$(function() {
  $("#tabs").tabs();
});
</script>

<div id="tabs">
  <!-- Get the categories to generate the interface -->
  <ul>
    <!-- We use a blank `enable=""` parameter in this tag because we are not interested in the channel entries -->
    {exp:nsm_categories:archive
      enable=""
    }
      <li><a href="#tabs-{category_id}">{category_name}</a></li>
    {/exp:nsm_categories:archive}
  </ul>

  <!-- Now build the content blocks for the tabs -->
  {exp:nsm_categories:archive
    enable="entries"
    target_category="13"
  }
    <div id="tabs-{category_id}">
      <p>{category_description}</p>
      <!-- List the channel entries assigned to the categories -->
      <ul>
      {if category_channel_entries_total_results}
        {category_channel_entries}
        <li class="{switch='one|two|three'}">
          <ul>
            <li>
              {category_channel_entry_count}: 
              <strong>{category_channel_entry_count}</strong> 
              of {category_channel_entries_total_results} 
              <small>{count} of {channel_entries_total_results} overall</small>
            </li>
            <li>{entry_title}: <strong>{title}</strong></li>
            <li>{entry_id}: <strong>{entry_id}</strong></li>
            <li>{entry_url_title}: <strong>{url_title}</strong></li>
          </ul>
        </li>
        {/category_channel_entries}
      {if:else}
        <li>No channel entries for this category</li>
      {/if}
      </ul>
    </div>
  {/exp:nsm_categories:archive}
  </div>
</div>

Render a nested category tree

{category_children_tagdata} recursively renders the tagdata until there are no more children.

<ul>
{exp:nsm_categories:archive 
  style="nested"
}
  <li>
    <strong>{category_name}</strong>
    <ul>
      <li>count: {count}</li>
      <li>category_depth: {category_depth}</li>
      <li>category_is_target_parent: {category_is_target_parent}</li>
      <li>category_is_target: {category_is_target}</li>
      <li>category_is_parent: {category_is_parent}</li>
      <li>category_children_total_results: {category_children_total_results}</li>
      <li>category_children_tagdata: {category_children_tagdata}
      {if category_children_total_results}
        <ul>
          {category_children_tagdata}
        </ul>
      {/if}
      {if category_children_total_results == 0}
        No children categories
      {/if}
      </li>
    </ul>
  {/category_children_tagdata}</li>
{/exp:nsm_categories:archive}
</ul>

channel_entries_query_end hook

You can improve performance when you use the category_channel_entry_limit parameter by adding an extension hook to the end of the Channel module's build_sql_query method.

function build_sql_query($qstring = '')
{
  …
  $this->sql .= $end;

  // Add this code to the end of the build_sql_query() method around line 2856
  // in /system/expressionengine/modules/channel/mod.channel.php
  if ($this->EE->extensions->active_hook('channel_entries_query_end') === TRUE) {
    $this->sql = $this->EE->extensions->call('channel_entries_query_end', $this, $this->sql);
  }
}

In the following example we are trying to return the first 5 channel entries for 10 categories that we have in our site.

<ul>
{exp:nsm_categories:archive 
  enable="entries"
  limit="99999"
  category_channel_entry_limit="5"
}
  <li>
    <strong>{category_name}</strong>
    <ul>
      {category_channel_entries}
        <li>entry_title: <strong>{title}</strong></li>
      {/category_channel_entries}
    </ul>
  </li>
{/exp:nsm_categories:archive}
</ul>

The Channel module will return up to 99999 channel entries that belong to any of the example site's 10 categories. The first 5 entries will be added to each category and the remainder will be ignored.

Of 99999 potential entries only 50 will be shown.

This is costly in terms of rendering performance because the database will have to send the data for up to 99999 entries, the server will process them all and then ignore the remaining entries. This is rather wasteful of server resources.

An alternative solution would be to alter the database request that gathers the channel entries data and perform the filtering earlier. This will prevent the server from processing vast amounts of data thanks to a streamlined database query.

The new extension hook added to the end of the build_sql_query method allows another add-on to manipulate the database query ExpressionEngine uses to get all channel entry data.

Now only the channel entry data for the 50 entries to be displayed will be returned from the database. This saves a lot of server resources from being wasted however still allows Expression Engine to decide what entries should be displayed on the page.

Releases RSS Feed © Copyright 2017 | Policies All prices shown are in Australian Dollars ($AUD) Newism