Page 1 of 1

Displaying "Related News" on News DetailPage

Posted: Thu May 24, 2007 4:26 pm
by Anastasis
On the draft of our new website design our news story pages have up to 3 items of related news at the bottom. These are just the headings of the news items and by clicking them the user is taken through to that news story.

In the past with our current static website we have maintained these links manually. However, with the possibility of migrating the site to CMSMS I can see that it should be possible to automate this by using the category assignment of each news story and getting the list of related news items from a custom news summary template.

Like the regular news summary template, the items will need to be in latest date order and have a limit of three items (or however many are selected).

Unlike the regular news summary template, the items listed must only be those that are "related" to the story (which should be possible by selecting only on category). Also, the list of related items must only be for stories that were published before the current story. So if the user is looking at an older story about say "Risk Management" from 31 December 2006 the related items listed should be from before 31 December and not include more recent items from the same news category (i.e. up to the current date).

What I am unclear about is how to do this. How do I code my summary template to meet the selection criteria above?

Thanks in advance for any help on this.

Re: Displaying "Related News" on News DetailPage

Posted: Thu May 24, 2007 4:38 pm
by calguy1000
Well, you can get 1/2 way there.  You can add a {News category=$entry->category} tag into your detail template, however, there's no way of specifying articles that are 'older' than the current one on the news tag. 

This would take a code modification.

Re: Displaying "Related News" on News DetailPage

Posted: Thu May 24, 2007 10:28 pm
by Anastasis
Thanks very much for your reply on this.

Hmm... sounds so close and yet so far. Would it be a simple change to the code to include this option?

I would assume the easiest way would be to include a pair of parameters to form a date range filter of "from" and "to" and when either one was empty to include everything at that at the end of the range or when both are empty (the default) to include everything (as now).

As it is already possible to only display items based on a category, would this not be a straightforward change to the code to also filter by date range?

However, I use the words "simple" and "straightforward" warily as having done very little coding in php, this may not be as simple as I am suggesting.

I have had a look at the php modules in the news section of CMSMS but cannot see where the category filtering is being handled. I would happily make this change myself if I knew how to and update the documentation accordingly, but I don't - unless someone can give me a steer on this. Do code changes have to be requested or can anyone make them with permissions?

Thanks again.

Re: Displaying "Related News" on News DetailPage

Posted: Fri May 25, 2007 8:18 am
by Anastasis
I have just thought of another reason having the ability to specify a date range for the news would be useful.

The news index pages for our current static website has a central column of news items (effectively a news summary page with summary text) and on the right-hand side an small index panel providing links to archived news items based on date periods. See here:

http://www.chasecooper.com/News-Regulatory.php

The main index contains news items up to 8 months old and the archived items are provided in four month tranches. When the main index of items grows into its ninth month, four months are sectioned off so to speak and they become the latest archive tranch

It would not be enough just to tag these items with, for example, an "archive" category. They need to be date specific.

Whilst what we do with related news as described at the start of this thread may be more unusual, I am sure that there must be a lot of websites that do archive older news items even if that is to only one long archive. Having the ability to specify a date range for display for this type of requirement would be a great addition.

I hope it is something that can be done.

Re: Displaying "Related News" on News DetailPage

Posted: Fri May 25, 2007 3:09 pm
by Anastasis
calguy1000 wrote: You can add a {News category=$entry->category} tag into your detail template
Just got this working - at least as you said halfway there without the date range- but thank you.

Found that I needed to make "News" to be "news", i.e. case sensitive.

I added the following into my detail template:

Code: Select all

{news category=$entry->category number='3' summarytemplate='related_news.tpl' dateformat='%e %B %Y'}
My related_news.tpl contains the following:

Code: Select all

<!-- Start Related News Display Template -->

{foreach from=$items item=entry}
  <li>{$entry->titlelink}</li>
{/foreach}

<!-- End Related News Display Template -->
which is OK apart from one issue, in that the list of related news items contains the news item of the page the summary is listed on.

Is there a way to conditionally filter out the duplicate by applying a conditional test into my summary template, e.g. by adding a line (in pseudo code) such as {if $detail-entry-title $summary-entry-title} ?

Re: Displaying "Related News" on News DetailPage

Posted: Fri May 25, 2007 3:19 pm
by calguy1000
Yes, you just have to do a bit of trickery.

In your detail template, you can do an {assign var='detail_title' value=$entry->title} (or something like that),

Then in your summary template, you can do something like:

{if $entry->title != $detail_title}
...
{/if}

Re: Displaying "Related News" on News DetailPage

Posted: Fri May 25, 2007 4:05 pm
by Anastasis
calguy1000 wrote: Yes, you just have to do a bit of trickery.

In your detail template, you can do an {assign var='detail_title' value=$entry->title} (or something like that),

Then in your summary template, you can do something like:

{if $entry->title != $detail_title}
...
{/if}
Oh yes it works great!  :)  Fantastic - thank you!

So if it is possible to pass the detail title across to the summary template could it be possible to also pass the article date and then test it conditionally against each of the summary items to be output?

Something like:

{assign var='detail_formatpostdate' value=$entry->formatpostdate}

but what code would I need to add to my summary template to filter on that?

I guess I would also need to amend the call to the summary template to not restrict the number of items but limit the count to a maximum of 3 in the summary template. Again how would I do that?

I am learning bits and pieces so it is getting easier to do the custom things along the way, but I am stil a long way from being where I want to be. It is very rewarding though. I appreciate your help.

Re: Displaying "Related News" on News DetailPage

Posted: Fri May 25, 2007 4:14 pm
by calguy1000
Yes, you can do the same thing with dates (theoretically), the only problem may come in with comparing the date strings.... You'd have to try it to be sure.... if comparing the strings doesn't work right off the bat, you may need to write either a smarty plugin, or a UDT that can convert the values back to unix timestamps for comparison.... but try just the string compares first.

As far as 'counting', and limiting the output to three or four articles, you could do something like:

{* outside the foreach *}
{assign var='count' value=0}

{* inside the foreach *}
{if $count < 3}
  ...
  {assign var='count' value=$count+1}
{/if}

Re: Displaying "Related News" on News DetailPage

Posted: Fri May 25, 2007 5:56 pm
by alby
Anastasis wrote:
So if it is possible to pass the detail title across to the summary template could it be possible to also pass the article date and then test it conditionally against each of the summary items to be output?

Something like:
{assign var='detail_formatpostdate' value=$entry->formatpostdate}

but what code would I need to add to my summary template to filter on that?
Try with:
{capture name=currentpostdate}{$entry->postdate|strtotime}{/capture}

And in detail template (in loop):
{if $entry->postdate
{/if}

Alby

Re: Displaying "Related News" on News DetailPage

Posted: Fri May 25, 2007 7:20 pm
by Anastasis
calguy1000 / alby

Many thanks to both of you - oh happy day! :)

I took on both of what you had said and adapted the following:

First in my detail template:

Code: Select all

<div id="news_story_related">
  {assign var='detail_postdate' value=$entry->postdate}
  {news category=$entry->category summarytemplate='related_news.tpl' dateformat='%e %B %Y'}
</div>
Then in my related_news.tpl

Code: Select all

<!-- Start Related News Display Template -->

{assign var='count' value=0} 
{foreach from=$items item=entry}
  {if $entry->postdate|strtotime < $detail_postdate|strtotime}
    {if $count < 3}
      {assign var='count' value=$count+1}
      {if $count == 1}
        <h2>Related News</h2>
        <ul>
      {/if}
      <li>{$entry->titlelink}</li>
    {/if}
  {/if}
{/foreach}
{if $count > 0}
  </ul>
{/if}

<!-- End Related News Display Template -->
...and it all works!

I haven't tested it in detail yet as I have only entered four items in my draft CMSMS news section, but you can see it here:

http://www.lunnlimited.co.uk/cmsmadesimple/

When viewed, the latest Markets item (top of the overall list) outputs the second Markets item (third in the overall list), but the second Markets item outputs nothing.

The only thing I wonder with the way I have done it is what looks a slightly messy way of having to test the count twice: initially to output the "Related News" heading and opening tag when the count = 1 and then close the tag pair if the count is > > 0 at the end.

It works but it doesn't look very elegant. Is there a better way?

Thanks again for your help with this - it has been great. :)