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 only one occurence of a date field

August 04, 2011 11:13am

Subscribe [5]
  • #1 / Aug 04, 2011 11:13am

    Mediakitchen

    261 posts

    I am displaying some entries from a channel that has the following fields:

    title
    news_item_date
    document_file

    I am only displaying the month and year of the date and this date is used as a heading.

    More than one entry may have the same date (month and year) and I would like to display the entries with just one heading.

    So instead of

    August 2011

    Title 1

    August 2011

    Title 2

    July 2011

    Title 3

    I want the following:

    August 2011

    Title 1

    Title 2

    July 2011

    Title 3

    Hence the month heading is only listed once for each month.


    My code is as follows:

    {exp:channel:entries  channel="business_immigration_news"  orderby="news_item_date" sort="desc"  limit="15"}
    
        <h3>{news_item_date format="%F %Y"}</h3><p>                </p>
    
    <p>    <h3 class="documentTitle"><a href="http://{document_file}" title="Download file" target="_blank" rel="noopener">{title}</a></h3>                    <br />
                    <br />
    {/exp:channel:entries}

    Any advice on how to achieve this would be appreciated.

    Thanks

    Paul

  • #2 / Aug 05, 2011 5:46am

    John Henry Donovan

    12339 posts

    hi Paul,

    take a look at the {date_heading} tag variable

    {exp:channel:entries  channel="business_immigration_news"  orderby="news_item_date" sort="desc"  limit="15"}
    
    {date_heading display="daily"}
    <h3>{news_item_date format="%F %Y"}</h3><p>     </p>
    
    <p>{/date_heading}<br />
                   </p>
    
    <p>    <h3 class="documentTitle"><a href="http://{document_file}" title="Download file" target="_blank" rel="noopener">{title}</a></h3>                    <br />
                    <br />
    {/exp:channel:entries}

  • #3 / Aug 05, 2011 6:48am

    Mediakitchen

    261 posts

    Thanks John

    I have had a look at this and it appears it uses the Entry Date as the basis for breaking up the entries. I need it to use my custom date variable (news_item_date) which I cannot see how to do using the date_heading.

    Look forward to your reply.

    Paul

  • #4 / Aug 08, 2011 12:02pm

    Sue Crocker

    26054 posts

    Hi, Mediakitchen.

    You can’t. You’d need to use the entry date in order for this to work.

  • #5 / Aug 08, 2011 12:07pm

    Mediakitchen

    261 posts

    Ah that is a shame as it was so close to being a perfect solution. Does this mean I may have to roll my own solution using PHP? If so, any tips? 😊

  • #6 / Aug 09, 2011 5:44pm

    Brandon Jones

    5500 posts

    Hi Mediakitchen,

    That, or perhaps a custom query that can group by the dates. I’ll move this over to Community Help to get you further input, as it’s a good question. Thanks!

  • #7 / Aug 10, 2011 9:28am

    Mediakitchen

    261 posts

    Thanks Brandon - a bit out my depth with SQL but perhaps the following will assist someone helping me come up with a solution:

    SELECT 
        DATENAME(mm, article.Created) AS Month, 
        DATENAME(yyyy, article.Created) AS Year, 
        COUNT(*) AS Total 
    FROM Articles AS article 
    GROUP BY 
        DATENAME(mm, article.Created), 
        DATENAME(yyyy, article.Created) 
    ORDER BY Month, Year DESC
  • #8 / Aug 10, 2011 11:44am

    Elemento, Inc.

    57 posts

    Hi Paul,

    For this instance I would use PHP, so enable PHP and use the following code:

    <? var $t = ""; //initialize variable ?>
    
    {exp:channel:entries  channel="business_immigration_news"  orderby="news_item_date" sort="desc"  limit="15"}
    <? 
    if($t == "{news_item_date format='%F %Y'}"){    }
    else { 
        $t = "{news_item_date format='%F %Y'}"; 
         echo "<h3>".$t."</h3><p>";<br />
    } ?><br />
                    <br />
    <h3 class="documentTitle"><a href="http://{document_file}" title="Download file" target="_blank" rel="noopener">{title}</a></h3>                    <br />
                    <br />
    {/exp:channel:entries}

    So, it will only post the date when it’s different from the previous date.

  • #9 / Aug 10, 2011 6:42pm

    Mediakitchen

    261 posts

    Thank you so much - that worked perfectly!! (apart from it didn’t like “var” so I just removed it)

    Really appreciate the help.

    Many thanks

    Paul

    p.s if anyone has same issue, be sure to set PHP Parsing Stage to Output

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

ExpressionEngine News!

#eecms, #events, #releases