Jump to content

How to find pages by template filename?


Recommended Posts


I want to list the latest articles on the site, the articles have multiple templates using the same template php archive.

I have tried with:

$pages->find('template=lvl04-post, limit=5');

but didn't work. Also, I have read about $template->filename, but don't understando how to use it with find()

Articles and parent categories have their own single templates because of role permissions, so I can't just do a normal 'template=name'

This is the full code:

<?php $posts = $pages->find('template=phpfilename?, limit=5');
foreach($posts as $post){ ?>
<a href="<?php echo $post->url; ?>"><?php echo $post->title; ?></a><br />
<?php echo date("d/m/Y", $post->published); ?><br/>
<?php echo $post->resume; ?><br/>
<?php } ?>

Any help is appreciated ?

Link to comment
Share on other sites


// Set filename manually
$filename = $config->paths->templates . 'lvl04-post.php';
// Or get filename from one of the templates that uses it
// $filename = $templates->get('lvl04-post')->filename;

// Find templates that use this template file
$tpls = $templates->find("filename=$filename");

// Find pages using those templates
$posts = $pages->find("template=$tpls, limit=5");


  • Like 5
Link to comment
Share on other sites

Sadly it didn't work, but I've already changed all the site and template structure so I can find by the single template name. I suppose is the best way, but would like to have the option to find by archive name, as it is an option in the template settings ?

Link to comment
Share on other sites

22 minutes ago, alanxptm said:

Sadly it didn't work

It works for me, so double-check your code.

Another way you can find related templates is to apply a tag to them in the admin (Advanced tab). Then you can find the templates that have that tag and find pages using those templates.

// Find templates with tag "foo"
$tpls = $templates->find("tags=foo");

// Find pages using those templates
$items = $pages->find("template=$tpls");


  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

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

  • Create New...