Skip to content

Instantly share code, notes, and snippets.

@jacmaes
Forked from Da-Fecto/this_n_this_and_that.php
Last active December 8, 2020 13:14
Show Gist options
  • Save jacmaes/9542fde33e19ea49e542 to your computer and use it in GitHub Desktop.
Save jacmaes/9542fde33e19ea49e542 to your computer and use it in GitHub Desktop.
this, this, this & that #pw
// In a function:
$count = count($pagearray);
foreach ($pagearray as $key => $p) {
$div = ($key + 1 === $count) ? ($count === 1 ? '' : ' & ') : ($key === 0 ? '' : ', ');
$out .= $div . "<a href='$p->url'>$p->title</a>";
}
echo $out;
// Direct output:
$count = count($article->author);
foreach ($article->author as $key => $auth) {
$list = ($key + 1 === $count) ? ($count === 1 ? '' : ' y ') : ($key === 0 ? '' : ', ');
echo $list . "<a href='$auth->url'>$auth->title</a>";
}
// With implode:
echo $page->blog_tags->implode(", ", "title");
$categories = $pages->get('1016')->children->implode(" &bull; ", "<a href='{url}'>{title}</a>");
echo $categories;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment