a-ok Posted March 17, 2017 Share Posted March 17, 2017 Hi folks, I have a simple news > article setup on a site I am building and at the bottom of the page I am outputting the latest 4 new articles... fairly simple. I will also add in so it doesn't include the current article you are viewing but ignore that for now. Also for each article, in the CMS, the client can choose if there is a related article they'd like to include (instead of the latest 4) up to a maximum of 4. So if they choose one related article it would include that, first, then continue with the latest 3. If they choose 2 related articles then it would show those two first then continue with the latest 2 and so on. I pretty much have the setup for this... see my code below... but I just wondered if this is the best way to do it? I have to output my HTML twice (one for the related articles and one for the latest) which is fine but I guess I'm wondering if there's a way where I don't have to (reducing extra effort by the server). Let me know what you think... I thought I'd post this in case it helps anyone else in the future although I realise the question is fairly bespoke. I'll also need to do a check that the selected related article isn't part of the 'latest posts' either! I'm also wondering if you can slice from the end (rather than from the start). Thanks! <?php $inspirations = $pages->find("parent=/inspiration/, sort=-global_published_date, limit=4"); ?> <?php if (count($page->global_inspiration_related)) : ?> <?php $curated_count = count($page->global_inspiration_related); $inspirations = $pages->find("parent=/inspiration/, sort=-global_published_date, limit=4")->slice($curated_count); ?> <?php foreach ($page->global_inspiration_related as $child) : ?> <!-- do something --> <?php endforeach; ?> <?php endif; ?> <?php foreach ($inspirations as $child) : ?> <!-- do something (same code as above thus outputting the HTML twice) --> <?php endforeach; ?> Link to comment Share on other sites More sharing options...
diogo Posted March 17, 2017 Share Posted March 17, 2017 I would probably do it like this (not tested): $curated = $page->global_inspiration_related; $nr_of_inspirations = 4 - count($curated); $inspirations = $pages->find("parent=/inspiration/, id!={$page}|{$curated}, sort=-global_published_date, limit={$nr_of_inspirations}"); $all = $curated->import($inspirations); 3 Link to comment Share on other sites More sharing options...
a-ok Posted March 17, 2017 Author Share Posted March 17, 2017 17 minutes ago, diogo said: I would probably do it like this (not tested): $curated = $page->global_inspiration_related; $nr_of_inspirations = 4 - count($curated); $inspirations = $pages->find("parent=/inspiration/, id!={$page}|{$curated}, sort=-global_published_date, limit={$nr_of_inspirations}"); $all = $curated->import($inspirations); Ah yes okay! Thanks for the help! I wonder what would happen if there are no $curated articles... I guess it wouldn't show anything (whereas it should return the latest 4). I could do the following... if (count($page->global_inspiration_related)) { $curated = $page->global_inspiration_related; $nr_of_inspirations = 4 - count($curated); $inspirations = $pages->find("parent=/inspiration/, id!={$page}|{$curated}, sort=-global_published_date, limit={$nr_of_inspirations}"); $all = $curated->import($inspirations); } else { $all = $pages->find("parent=/inspiration/, id!={$page}, sort=-global_published_date, limit=4"); } Link to comment Share on other sites More sharing options...
diogo Posted March 17, 2017 Share Posted March 17, 2017 If there's nothing in curated, then $curated would be an empty page array, which is perfectly fine because it would just get filled with the 4 regular inspirations. The only doubt that I have is if the import will keep the order of the pages. Link to comment Share on other sites More sharing options...
a-ok Posted March 17, 2017 Author Share Posted March 17, 2017 1 hour ago, diogo said: If there's nothing in curated, then $curated would be an empty page array, which is perfectly fine because it would just get filled with the 4 regular inspirations. The only doubt that I have is if the import will keep the order of the pages. I've just tested it... looks good! Link to comment Share on other sites More sharing options...
SamC Posted March 18, 2017 Share Posted March 18, 2017 On 17/03/2017 at 11:13 AM, oma said: Also for each article, in the CMS, the client can choose if there is a related article they'd like to include (instead of the latest 4) up to a maximum of 4. @oma I like the sound of this, if you don't mind me asking, how is this setup in the CMS, with a pageField or something? A screenshot of the edit page would be awesome. Link to comment Share on other sites More sharing options...
diogo Posted March 18, 2017 Share Posted March 18, 2017 2 minutes ago, SamC said: @oma I like the sound of this, if you don't mind me asking, how is this setup in the CMS, with a pageField or something? A screenshot of the edit page would be awesome. This is easily done with a page reference field. Here is a very old video demonstrating it https://processwire.com/videos/page-fieldtype/. The field became much more complete meanwhile, but the basics are there. 1 Link to comment Share on other sites More sharing options...
SamC Posted March 18, 2017 Share Posted March 18, 2017 2 minutes ago, diogo said: This is easily done with a page reference field. Here is a very old video demonstrating it https://processwire.com/videos/page-fieldtype/. The field became much more complete meanwhile, but the basics are there. Ah, so that's what PageListSelectMultiple does! Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now