Jump to content

Recommended Posts

Posted

Hey I'm building my first new site in well over a year and am a little rusty on selectors but particularly retrieving sub-fields of selected pages.

I am trying to output the meta data of a blog post as follows.

Quote

Posted by [Person Name], [Job title].

[Person Name] is just a field with a Page Reference and simple enough.
[Job Title] is the sub-field within the page that was referenced above.

I actually have it working with the following:

Posted by:

<?php
	if($page->insight_author) {
	echo $page->insight_author("<a href='{url}'>{title}</a>");}
?>
,
<?php
	$roles = $page->insight_author;
	foreach ($roles as $role) {
	echo "{$role->staff_role}";} 
?>

but was wondering how to do this with selector sub-selectors instead. My current code is probably quite 'old school'?

Thanks

 

Posted

Isn't your code missing a bit for $roles ? Right now, and if not, you could just do

echo $page->insight_author("<a href='{url}'>{title}</a>, {staff_role}");

 

Posted
Posted by:

<?php if($page->insight_author): ?>
	<a href='<?= $page->insight_author->url ?>'><?= $page->insight_author->title ?></a>
<?php endif ?>
,
<?php foreach ($page->insight_author->roles as $role): ?>
	<?= $role->staff_role ?>
<?php endforeach ?>

I would suggest something like the above, though there are certainly other templating styles. Like @monollonom said, I'm not exactly sure what role represents or how that is structured.

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