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.

A more efficient way to call categories

October 03, 2011 9:20am

Subscribe [2]
  • #1 / Oct 03, 2011 9:20am

    ralph.m

    225 posts

    I have a thumbnail gallery that is embedded on various pages. Different thumbnails are displayed on each page depending on the selected category.

    <ul class="gal">{exp:channel:entries channel="gallery" disable="trackbacks|member_data|pagination" category="3" sort="asc" dynamic="no"}
        <li class="{switch="rbmgn|rbmgn|bmgn"}">
            <a href="http://{title_permalink=prints/view}">_            {pri-gal-thumb}_        </a>
        </li>{/exp:channel:entries}
    </ul>

    However, currently I have several of these embeds, the only difference being the category tag. This seems really inefficient, and I’m sure there must be a better way.

    Is there some way to use all that code once—say in a single embed—and set conditions depending on which category should show? Or something similar?

    Thanks for any suggestions. 😊

  • #2 / Oct 03, 2011 10:42am

    Boyink!

    5011 posts

    What are the URLs that these galleries are appearing at?

    A couple of ideas:

    - If the URL has category information in it then it’s possible you could leave this tag running dynamically and stop forcing the category id.

    - Otherwise create an embedded template with this code and pass the category id as an embed variable from the parent template.

    - Use conditionals to look at the URLs and set the correct category id—all in one embedded template or snippet.

  • #3 / Oct 03, 2011 7:16pm

    ralph.m

    225 posts

    Thanks for your reply, Mike. I wish I could ask more intelligent questions, but I really struggle to conceptualize how EE works (beyond the basics). I stare for hours at variables like category_id without the faintest clue what to do with them, which make learning EE very hard, I find.

    What are the URLs that these galleries are appearing at?

    Because each category needed its own meta info (description, keywords etc.) I’ve set up a separate channel entry for each page that has it own gallery. (During publishing, a category is selected from a list to associate a category with an entry.) Then I use conditionals in the template to pull in different embeds (such as the one posted above) depending on the segment_2 of the URL.

    I’m sure this is hopelessly inefficient, though. I’ve read that you can set up custom category fields, but I don’t know how. Could these replace the meta fields I need on the page?

    If the URL has category information in it then it’s possible you could leave this tag running dynamically and stop forcing the category id.

    At the moment the URL doesn’t really have cat info in it, but I’m not sure how I’d use that anyhow. Would it use category_id? I still don’t get how to display something based on the cat info in the URL.

    Otherwise create an embedded template with this code and pass the category id as an embed variable from the parent template.

    I was wondering about that, but wasn’t sure how. Can you use embed variables in this way?

    Use conditionals to look at the URLs and set the correct category id—all in one embedded template or snippet.

    I guess that’s basically what I’m after. Just not sure how to do it. How would you do that with the code I posted above?

    Thanks for your help.

  • #4 / Oct 03, 2011 8:21pm

    Boyink!

    5011 posts

    Well, backing way up to the concept of having per-entry photo galleries.  The most efficient way I know if is to use the Matrix Add-on from Pixel and tonic. Matrix allows you to add multiple, variable number of rows of things to one entry.

    I’m using it here on our travel site:
    http://boyinks4adventure.com/our-travels/badlands/

    Everything you see on that page - the text, the photos, the videos and the map are all contained in one entry.  Collectively it all gets stored in one channel.

    What that doesn’t allow is a gallery to be re-used across posts, which your approach does allow (not sure if that was a requirement).

    - EE can use either category id or category url titles in the URL.  Thats a choice you make as developer.

    - You could possibly use category custom fields to store meta information.

    - One thought is to do the following.  Move your gallery display code into it’s own template (I’ll call it ‘display_category_thumbs’).  Tweak the code to be the following:

    <ul class="gal">{exp:channel:entries channel="gallery" disable="trackbacks|member_data|pagination" category="{embed:my_category_id}" sort="asc" dynamic="no"}
        <li class="{switch="rbmgn|rbmgn|bmgn"}">
            <a href="http://{title_permalink=prints/view}">_            {pri-gal-thumb}_        </a>
        </li>{/exp:channel:entries}
    </ul>

    Note how I’ve removed the hardcoded category info and replaced it with a variable. That variable needs to get passed from the parent loop.  You didn’t post that code so let me make some up:

    {exp:channel:entries channel="content_page"}
      {title} {!-- standard content fields --}
      {body}
    
      {if {page_gallery !-""} {!-- Look to see if a gallery id has been entered on this page}
        {embed="embeds/display_category_thumbs" my_category_id="{page_gallery}"} {!-- call in the embedded template but pass the id of the gallery to display as an embed variable for embedded templates --}
      {/if}
    {/exp:channel:entries}

    Note the embed will get called once for every iteration of the loop - so this approach is fine on a single entry page. If the parent template was displaying >1 entry from the “content_page” channel you’d get >1 galleries returned.

  • #5 / Oct 03, 2011 8:40pm

    ralph.m

    225 posts

    Ah Mike, thanks very much for your detailed reply. I really appreciate it.

    I know there must be lots of good ways to do this, but your suggestion of

    category=”{embed:my_category_id}”

    is what I was looking for in terms of keeping what I have. I just wasn’t quite sure how to mix embed variables with the category parameter, but you’ve shown how above. Just wasn’t sure if it could be used inside the channel tags like that, but I’m delighted that it can be.

    The most efficient way I know if is to use the Matrix Add-on

    I am actually using Matrix, but not in this instance. Basically, the client sells lots of framed prints. For each one there’s an channel entry that includes a gallery of images (views of the print mounted, framed etc) which is done with Matrix. But each entry also has a single thumbnail image (used to link to the entry), and it’s all these thumbnail images that I’m organizing. Prints are gathered into categories, so I’m filtering out these thumbnails by category.

    You could possibly use category custom fields to store meta information.

    I was wondering that. I can’t find custom cat fields mentioned in the docs, though, and I can’t see a place to create them. Do you know of a link that explains how to set these up?

    Once again, thanks very much for your explanation. 😊

  • #6 / Oct 03, 2011 8:51pm

    Boyink!

    5011 posts

    http://ellislab.com/expressionengine/user-guide/cp/admin/content_admin/category_field_management.html - but it’s pretty straightforward if you have created channel fields.  You just don’t get the whole vocabulary of fieldtypes at the category level.

  • #7 / Oct 03, 2011 9:48pm

    ralph.m

    225 posts

    Ah, thanks Mike. In EE2 the path seems to be Admin > Channel Admin > Categories > Manage Category Fields. Don’t know how I missed that. This will be very handy. Thanks again.

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

ExpressionEngine News!

#eecms, #events, #releases