4

Okay so on my Homepage I want to pull out 3 lists of news items; major news, featured news and news headlines. There should only be one item shown under the Major News heading at a time and any other entries that are flagged as such will filter back into featured news list.

My problem is capturing the entry_id of the 'one' major news item and excluding it from the featured list. The major news item might not always be latest news item so that rules out using offset="1".

So I put together some Stash sets that filter out the news based on the custom field (cf_news_article_type). cf_news_article_type is a list of 3 radio buttons that the user sets when adding a news item. The values of the fields are set up as follows:

mj : Major Feature
gf : Featured Article
gn : General article

I stash a set of 13 news items from all types:

{!-- Bring out all the recent news --}
{exp:channel:entries channel="news" orderby="date" sort="desc" dynamic="no" limit="13" parse="inward" disable="categories|category_fields|member_data|pagination|trackbacks"}
{exp:stash:append_list name='recent_articles'}
    {stash:item_count}{count}{/stash:item_count}
    {stash:item_type}{cf_news_article_type}{/stash:item_type}
    {stash:item_title}{title}{/stash:item_title}
{/exp:stash:append_list}
{/exp:channel:entries}

Then later in my template I pull out the major news item:

<article class="major-feature">
{exp:stash:get_list name="recent_articles" match="#mj#" against="item_type" limit="1" parse_tags="yes"}
    {sn_news_story}
{/exp:stash:get_list}
</article>

Now the big question: how can I exclude that single major news item from the featured news list, but keep the other items flagged 'mj' when I get the featured Stash list?

<section class="featured-stories">
<h1>Featured stories</h1>
{exp:stash:get_list name="recent_articles" match="#(mj|gf)#" against="item_type" limit="3" parse_tags="yes"}
    {sn_news_story}
{/exp:stash:get_list}
</section>

Then for my headlines I'd love to exclude all the featured and Major news items that are shown in these two lists, but I will tackle that one in another post I think.

Is this even the correct approach for doing such a thing? Any help would be greatly appreciated.

since1976
  • 222
  • 1
  • 6
  • Also, would be awesome if you could support our proposal for a dedicated EE stack exchange site here. If you're already supporting it, you need to make sure your accounts are linked (use the same email address). http://area51.stackexchange.com/proposals/46387/expressionengine?referrer=AwnV9oYF5EKlETXKp3ZQQw2 – Adrian Macneil Oct 31 '12 at 22:29
  • Is there more than one article set with mj at one time? If so, how do you determine which one to pull out for the major feature? – Tyssen Oct 31 '12 at 22:59

1 Answers1

1

How about stashing your featured news article separately? For example:

{!-- find just the featured article --}
{exp:channel:entries channel="news" orderby="date" sort="desc" dynamic="no" search:cf_news_article_type="mj" limit="1" parse="inward"}
    {exp:stash:append_list name='featured_article'}
        {stash:item_count}{count}{/stash:item_count}
        {stash:item_type}{cf_news_article_type}{/stash:item_type}
        {stash:item_title}{title}{/stash:item_title}
    {/exp:stash:append_list}
    {exp:stash:set_value name="featured_article_id" value="{entry_id}" type="snippet"}
{/exp:channel:entries}

Then when you find the rest of the news, just ignore the featured article you already stashed:

{!-- find the rest of the news --}
{exp:channel:entries channel="news" orderby="date" sort="desc" dynamic="no" limit="12" entry_id="not {featured_article_id}" parse="inward"}
    {exp:stash:append_list name='recent_articles'}
        {stash:item_count}{count}{/stash:item_count}
        {stash:item_type}{cf_news_article_type}{/stash:item_type}
        {stash:item_title}{title}{/stash:item_title}
    {/exp:stash:append_list}
{/exp:channel:entries}

(I'm not sure whether you will run into parse order issues with the {featured_article_id}, but if so there should be a way around it)

Alternatively, if you don't mind all your featured news articles being at the top of the list, you could use orderby="cf_news_article_type|date", then just use limit="" and offset="" to get the first news article off the list.

UPDATE: You could do this the other way around too, and check the entry_id when you retrieve the list:

<article class="major-feature">
    {exp:stash:get_list name="recent_articles" match="#mj#" against="item_type" limit="1" parse_tags="yes"}
        {exp:stash:set_value name="featured_article_id" value="{entry_id}" type="snippet"}
        {sn_news_story}
    {/exp:stash:get_list}
</article> 

<section class="featured-stories">
    <h1>Featured stories</h1>
    {exp:stash:get_list name="recent_articles" match="#(mj|gf)#" against="item_type" limit="3" parse_tags="yes"}
        {if "{entry_id}" != "{featured_article_id}"}
            {sn_news_story}
        {/if}
    {/exp:stash:get_list}
</section>
Adrian Macneil
  • 13,017
  • 5
  • 57
  • 70
  • The stash items inside the list are just a few of the many that I have, so was trying to not repeat myself. Also was trying to work out a way to do it with just one call. Do you know if it is possible to put that list of stash items into a snippet to be reusable? – since1976 Oct 31 '12 at 22:29
  • Sorry, I'm still a bit of a stash-noob. What about stashing the featured entry_id when you retrieve the featured article instead, then using it in a conditional inside your snippet for the rest of the articles? – Adrian Macneil Oct 31 '12 at 22:36