2

I have content entries in a channel called resources.

Resources primary categories are whitepapers and downloads.

Secondarily, the entries are categorized by "brand"

What I want to do is grab the current category id (e.g. downloads) but then display the entries grouped by "brand"

This is my code.

{exp:channel:category_heading channel="resources"}
{!--grab main category we're viewing. E.g. we're viewing "downloads" in the "resources" channel--}
<?php $category = '{category_id}'; ?>
{/exp:channel:category_heading}

{!-- loop through the brands --}
{exp:channel:categories category_group="2" show_empty="no"}

    <h2 style="float:left; clear:both; width:100%;">{category_name}</h2>

    {!-- now display entries from this main category (e.g. downloads) that are also categorized under the current brands --}
    {exp:channel:entries category="<?php echo $category; ?> & {category_id}" channel="resources" disable="member_data|pagination|trackbacks"}

        <a href="{pdf}">{title}</a><br>

    {/exp:channel:entries}

{/exp:channel:categories}

Where it says $category and category_id, only recognizing the latter. I can accurately print the $category to screen in the loop but the category= filter doesn't recognize it.

Thoughts?

2 Answers 2

0

It looks like you have a mix of using php on input and output in the same template. Output while setting $category (EE tags run first, then output {category_id} for use in php), and input while echoing $category (PHP runs first and outputs $category as a string for use in an EE tag).

So you'll only be able to set the template for one or the other. You could use something like Stash to set the variables instead of php, or move the interior channel entries to an embedded template.

UPDATE:

{exp:stash:set name="main_category_id" parse_tags="yes" trim="yes"}
    {exp:channel:category_heading channel="resources"}
        {category_id}
    {/exp:channel:category_heading}
{/exp:stash:set}

{exp:channel:categories category_group="2" show_empty="no"}
    <h2 style="float:left; clear:both; width:100%;">{category_name}</h2>

    {exp:channel:entries 
        category="{exp:stash:get name='main_category_id'}&{category_id}" 
        channel="resources" 
        disable="member_data|pagination" 
        status="not closed" 
        dynamic="no" 
        parse="inward"
    }
        <a href="{pdf}">{title}</a><br>
    {/exp:channel:entries}
{/exp:channel:categories}
3
  • nice screen name. I will look into this and update if this resolve it for me.
    – JaseC
    Jul 8, 2013 at 3:16
  • OK, Stash doesn't seem to be working either. In category_heading I have {exp:stash:set name="main_category_id"}{category_id}{/exp:stash:set} then in channel:categories I can print {exp:stash:get name="main_category_id"} to screen but I can't use it in the category= filter. My test category is '42'. As a control, if I just put that number in the category= filter, it works. I've tried switching PHP processing modes to no avail.
    – JaseC
    Jul 8, 2013 at 5:48
  • It sounds like the way you're headed with Stash is the way to go, but the parsing order is a problem. Try adding parse="inward" to your channel entries parameters. I'm updating the answer with an example. Jul 8, 2013 at 16:47
1

I would construct the filter/entry relationship differently to avoid nesting a channel entries loop inside the channel categories loop.

SOLUTION REQUIRES:

Pixel & Tonic's Playa add-on (http://docs.pixelandtonic.com/playa/)
Low's Seg2Cat add-on (http://gotolow.com/addons/low-seg2cat/docs)

CHANNELS

• Resources {resources}
• Brands {brands}

RESOURCE CATEGORY GROUP

• Resource Type
   - Downloads {downloads}
   - White Papers {white-papers}

RESOURCE CUSTOM FIELDS

• Resource Asset {resource_asset}
• Resource Brand {resource_brand}
   - Type: Playa
   - Field Label: Brand
   - Field Short Name resource_brand
   - Is this a required field? YES
   - Allow multiple selections? NO
   - Show expired entries? YES|NO
   - Show future entries? YES|NO
   - Channels: Brand
   - Categories: Any
   - Authors: Any
   - Statuses: Open
   - Limit entries to: All
   - Order entries by: Entry Title in Ascending Order

TEMPLATE CODE

http://www.example.com/resources/downloads/

{exp:channel:entries channel="brands" dynamic="no" disable="..."}
    {if {exp:playa:total_parents} > 0}

    <h1>{title}{if segment_2} > {segment_2_category_name}{/if}</h1>

    {exp:playa:parents channel="resources" category="{segment_2_category_id}"}
        <a href="{resource_asset}">{title}</a><br />
    {/exp:playa:parents}

    {/if}

{/exp:channel:entries}

I have not tested the overhead for this solution, so I do not know how it would compare to the Stash solution above.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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