Jump to content

Recommended Posts

Posted

Hi guys, looking for some help here. I'm trying to build some custom code so that I can display a list of 'related news/articles' that's associated with a particular Page record. I'm using the Database Relationship fields to make this connection work (I have two databases: "News", and "Info", and on the News database I have the Database Relationship field to enter a page from the Info database if the news article is related to it).

On the related Info Page, I want to return three variations of a list of articles from the News database:

  1. A list of News articles that happens to be in the 'Features' category (ID is 10), and happens to have the prefix "Reviews"
  2. A list of News articles that happens to be in the 'Features' category (ID 10) but NOT have a "Reviews" prefix
  3. A list of News articles that happens to NOT be in the 'Features' category at all (other News category IDs are 1, 8, 16 and 17)

I've managed to get quite far using the getReciprocalItems code - at the moment I am looking at something like this:

<h2 style="margin-top: 0;">Reviews</h2>

{{if $records = $record->getReciprocalItems()}}
	{{foreach $records as $fieldId => $items}}
		{{if $fieldId == '6'}} <!-- Where '6' is the ID of the 'News' Database -->
			<div class='ipsClearfix ipsPadding' style="padding-top: var(--sp-2) !important;">
				<ul class="ipsList_inline ipsList_csv ipsList_noSpacing" style="list-style: initial;">
					{{foreach $items as $item}}
						{{if $item->container()->_id == '10' AND $item->prefix() == 'Reviews' }} <!-- Where '10' is the ID of the 'Features' Category Within the 'News' Database -->
							<li style="width: 100%;">
								<div class="ipsCommentCount ipsPos_right" style="border-radius: 7px;" data-ipstooltip="" _title="{lang="num_comments" pluralize="$item->record_comments"}">{$item->record_comments} <i class="fa fa-comments"></i></div>
                              					<a href="{$item->url()}"><img class="ipsImage" src="{file="$item->record_image"}" style="width: 100px; border-radius: 5px; margin-right: 10px;" /></a>
								<a href="{$item->url()}">{$item->_title}</a> - {datetime="$item->record_publish_date"} 
							</li>  
						{{endif}}
					{{endforeach}}
				</ul>
			</div>
		{{endif}}
	{{endforeach}}
{{endif}}

The major problems I have, that I'd like to try and solve, are as follows:

  1. While the above code works, I'm sure it's massively inefficient. If I understand it correctly, I'm calling all 'News' database records (line 5) before I get the chance to filter down to "only those in category ID 10 and has prefix 'Reviews'" (line 9). Would that put a massive strain on the database if there are a lot of entries to call? I'd like to be able to GET only the records in that News database that meets the category/prefix criteria from the start. However, everything I try and do causes a terminal error. Can someone help make sense of that for me?
     
  2. This kind of ties in with the above issue. I want to make it so that the H2 header ("Reviews") and the DIV and UL tags do not appear if there are no records meeting the "News database + Features category + Reviews prefix" criteria. At the moment, because the code only seeks records that happen to be in the News database (line 5), it is also returning the DIV and UL tags before hitting the wall in line 9 and ceasing to print the content. Again, I've tried to include additional statements to a number of the first lines of code (using AND statements) but it's totally breaking the site.
     
  3. This relates to objective #3 above (A list of News articles that happens to NOT be in the 'Features' category at all). The code above works in returning only News articles that happen to be in container ID 10 ('Features' category) and with a prefix 'Reviews', but I have tried to replicate the code to apply to different categories/containers and it doesn't seem to be working. I have tried:
     
{{if $item->container()->_id == '1'}} <!-- '1' is the ID of the 'Regular' News Category -->

and I have tried:

{{if $item->container()->_id == '1' || '8' || '16'}} <!-- '1/8/16' is the ID of the 'Regular/Games/Media' News Categories -->

But they seem to return all articles (including those from the 'Features' (ID 10) category). I've also tried:

{{if $item->container()->_id != '10'}} <!-- '10' is the ID of the 'Features' News Category -->

But this seems to just not work at all.

Would love to hear your thoughts! Thanks so much.

  • Recently Browsing   0 members

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