Jump to content

Get page data from an array of pages, whats the quickest method


Recommended Posts

Posted

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.

Posted
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.

Posted

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.

  • Like 2
Posted

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. 

  • Like 5

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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