Jon E Posted July 16, 2015 Posted July 16, 2015 Hi, I have created a page field type called related_projects. I want to output this on a template. I need to limit it to 2 and to: - Find the project name - The first image from a field named images Can anyone help me please? I've really had a good look but totally stuck, especially with the image... Thanks so much. Jon
Macrura Posted July 16, 2015 Posted July 16, 2015 you mean like this? <?php $relatedProjects = $pages->find("template=project,limit=2"); foreach($relatedProjects as $rp) { echo "<h2>{$rp->title}</h2>"; echo '<img src="' . $rp->images->first()->url . '" alt='' />'; } 2
Jon E Posted July 17, 2015 Author Posted July 17, 2015 Hi, Not sure that will work, I need to find the pages selected in my related_projects field... Thanks though...
Macrura Posted July 17, 2015 Posted July 17, 2015 $relatedProjects = $page->related_projects->find("limit=2");
Jon E Posted July 17, 2015 Author Posted July 17, 2015 Thanks looks like that would work but.... Parse Error: syntax error, unexpected '' . $rp->images->first()->url ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' (line 39 of
Jon E Posted July 17, 2015 Author Posted July 17, 2015 Hi, Got it working with the below, thanks so much. Does anyone know how I can only show the #related_projects div if I have options selected? Seems to be showing on all pages currently. Sorry to be annoying and thanks so much. Jon <?php if($page->related_projects):?> <div id="related_projects"> <h2>You might like...</h2> <?php $relatedProjects = $page->related_projects->find("limit=2"); foreach($relatedProjects as $rp) { echo "<li><a href='{$rp->url}'><img src='{$rp->images->first()->url}'>"; echo "<h3>{$rp->title}</h3></a></li>"; } ?> </div> <?php endif; ?>
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