Nietzsche
Nietzsche

Reputation: 349

cffeed function: show just most recent post?

We have a function to pull RSS feeds and display the post on a ColdFusion page using this code:

<cfset rssUrl = "rss1">
<cffeed action="read" source="#rssUrl#" query="fitness" properties="info">

<cfset rssUrl2 = "rss2">
<cffeed action="read" source="#rssUrl2#" query="nutrition" properties="info">

<cfif #fitness.PUBLISHEDDATE# gt #nutrition.PUBLISHEDDATE#>
<cfset entries="fitness">
<cfelse>
<cfset entries="nutrition">
</cfif>

Output done via:

<cfoutput query="#entries#">

Problem is, the RSS feed has several posts and we only want to show one. Any thoughts on how to get it to pull and display only the most recent post? (We want the feed to have multiple posts, so right now our non ideal solution is to set maximum posts per feed to 1)

Upvotes: 2

Views: 461

Answers (2)

Raymond Camden
Raymond Camden

Reputation: 10857

cfoutput/query=".." will go over an entire query. If you only want to do the first row, use:

<cfoutput>
Title from row 1: #somequery.title[1]#
</cfoutput>

Basically - array notation on the column. Make sense?

Upvotes: 2

ale
ale

Reputation: 6430

There's nothing wrong with Ray's answer, but here are some other options.

<cfoutput query="#entries#" maxrows="1">

Offers the least disruption to your existing code and, should you decide to change the number of rows displayed (like, via a user setting) it's an easy change.

OR

If you copy the query object rather than the query name (which isn't actually a copy but a copy by reference)

<cfset entries = fitness>

instead of

<cfset entries = "fitness">

you can do this

<cfoutput>
  #entries.columnName1#
  #entries.columnName2#
  <!--- etc. --->
</cfoutput>

which will, by default, display only the first row of the query.

Upvotes: 2

Related Questions