Martinus Posted August 15, 2022 Share Posted August 15, 2022 Hello, I try to display my field value from a page but it only shows: 1020->title On my product page I use $product->vendor->title and displays just fine: Samsung My code: <ul> <?php $items = $pages->find("parent=/products/, limit=3, sort=-date"); foreach($items as $item) { echo "<li>" . "<h3><a href='$item->url'>$item->title</a></h3>" . "<p>$item->vendor->title</p>" . "<p>$item->summary</p>" . "</li>"; } ?> </ul> What I do wrong? Link to comment Share on other sites More sharing options...
zoeck Posted August 15, 2022 Share Posted August 15, 2022 Try {$item->title}, does it work then? "<h3><a href='{$item->url}'>{$item->title}</a></h3>" . Link to comment Share on other sites More sharing options...
Jan Romero Posted August 15, 2022 Share Posted August 15, 2022 Enclose the variable parts in curly braces: "<p>{$item->vendor->title}</p>" Otherwise it will only evaluate the first property, $item->vendor. Because it’s a page, you get its ID. It’s bugging me that I can’t seem to find any mention of this limitation in the PHP docs. It only says it will “greedily take as many tokens as possible to form a valid variable name”, but apparently it’s not so greedy with sub-properties? Anyway, I simply always use curly braces… Btw, if you use an editor with a language server such as PHP Intelephense, the syntax highlighting will show you the problem: 1 Link to comment Share on other sites More sharing options...
Martinus Posted August 15, 2022 Author Share Posted August 15, 2022 Yep! This did the trick: "<p>{$item->vendor->title}</p>" Is this always when taking multiple pages in a selector? Link to comment Share on other sites More sharing options...
Jan Romero Posted August 15, 2022 Share Posted August 15, 2022 It’s a PHP feature, it has nothing to do with ProcessWire or its selectors: https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing I just wouldn’t bother with the curlyless syntax. 1 Link to comment Share on other sites More sharing options...
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