cb2004 Posted October 3, 2016 Posted October 3, 2016 On a homepage I have got some calls to action and I was just wondering whats the most optimised method. Does PW have this in the API? <?php $ctas = [ 1038, 1039, 1040, ]; foreach ($ctas as $cta): $p = $pages->get("$cta"); ?> <div class="home-cta"> <a href="<?= $p->url ?>"> <img src="<?= $p->image_cta->url ?>"> <h2><?= $p->get("text_1|title") ?></h2> </a> </div> <?php endforeach; ?> Cheers.
BitPoet Posted October 3, 2016 Posted October 3, 2016 There's $pages->getById(): <?php $ctas = ...; foreach($pages->getById($ctas) as $p): 2
SamC Posted October 3, 2016 Posted October 3, 2016 2 hours ago, BitPoet said: There's $pages->getById(): <?php $ctas = ...; foreach($pages->getById($ctas) as $p): This is most interesting. I have similar calls to action and was going to create new pages with a link field in order to link to other pages, then just loop through them. But since I know the IDs, and the calls to action are unlikely to change much, this is both quicker and easier Thanks.
Robin S Posted October 3, 2016 Posted October 3, 2016 You can also use page IDs in a $pages->find() selector... $ctas = "1234|1235|1236"; foreach($pages->find("id=$ctas") as $p): ...but $pages->getById() has the advantage that the resulting PageArray preserves the order of IDs as you supplied them. 2
Soma Posted October 4, 2016 Posted October 4, 2016 It should be mentioned that the getById() is used internal and not access or status aware. So it's like get(id) and will load the page(s) even if they're unpublished or the user has no view rights, all a $pages->find(...) does. 5
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