Jump to content

Pinned records first in record feed template


Meddysong

Recommended Posts

I use a couple of record feeds on the front page of my site to provide the latest articles from a couple of databases. Unfortunately, pinned records aren't showing at the top.

I've looked into various database templates and can't see that there's any specific code telling the system to output pinned or featured records first as rows and then work through the rest. The system seems to know it's got to do this without the template telling it to.

This isn't the case with the record feed and I haven't seen a setting asking it to acknowledge that pinned will come first. I suppose there's no need; that's really not what a feed is used for.

Clearly I need to edit a template for the feed block. Is there any shorthand to do this?

Link to comment
Share on other sites

But in that case there would be a gap on the page if there weren't enough records to fill a row.

I'm working on the solution at the moment; I remember in a database on another site wanting the pinned records to be in a separate box and so having to set and unset them. I'm just looking at the templates to see how I did it; I'll post the solution once I've worked it out 🙂

Link to comment
Share on other sites

OK, got it. There's a lot of repitition in here so it's not very efficient and won't be quite so easy to update. The feed I'm going to use this template with will never be used vertically, which is good, otherwise I'd have to make even more amendments. Here's how I solved it:

1) You have to identify which records in the feed are pinned.

Usually the foreach loop starts off as

{{foreach $records as $record}}

This needs to be changed to

{{foreach $records as $id => $record}}
	{{if $record->mapped('pinned')}}

Since you've introduced an if-clause, you need to make sure there's an {{endif}} before you close your foreach loop after your output code.

2) You need to run the foreach loop again, this time with the pinned records removed.

This is the code which does the magic:

{{foreach $records as $id => $record}}    		
	{{if $record->mapped('pinned')}}
    	{{$pinned[] = $record;}} 
		{{unset($records[$id]);}}
    {{endif}}
{{endforeach}}

3) Replace the original foreach loop with

{{foreach $records as $id => $record}} 

So, whereas before you had

- foreach loop

- output code

- end foreach loop

you now have

- foreach loop

- specify you want pinned posts only

- output code

- end foreach loop

- foreach loop

- remove the pinned posts from your list of records

- end foreach loop

- foreach loop

- output code (same as before), which uses the new revised list of records

- end foreach loop

Phew!

Link to comment
Share on other sites

No, scratch this, I don't think it's going to be possible with this approach. I've just noticed that one of my pinned topics has disappeared. The feed is set to show 6 topics and it wasn't among them; my thinking is that the feed generated the six most recent (a record with new comments counting as more recent that a newer record but without the comments) and one of the pinned ones wasn't among them so it wasn't included over all. That makes sense.

I think I could build on what opentype suggested earlier; have a feed just for pinned and another for unpinned. I could do some special templates for them which remove the titles etc and just have the rows generated, then embed both of those within another template which has the formatting. That's all for another day though!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...