So I’m trying to build a menu which outputs all the child page’s tags but I can get anything to work. It seems like it should be simple but I’m flummoxed. I have it so that I can get the tags of the current page, but I would like the tags of the child pages.
This is what I have:
<ul>
<?php foreach(str::split($page->tags(),',') as $tag): ?>
<li><a href="#" class="<?php echo $tag ?>"><?php echo $tag ?></a></li>
<?php endforeach ?>
</ul>
To get all the tags from all the child pages, you can use the pluck()
method:
<ul>
<?php
$tags = $page->children()->visible()->pluck('tags', ',', true);
foreach($tags as $tag): ?>
<li><a href="#" class="<?php echo $tag ?>"><?php echo $tag ?></a></li>
<?php endforeach ?>
</ul>
Thank you. This works to get child tags but now I have duplicate tags because some of the child pages contain dupe tags. Is there a way to just output used tags once only. It can be a site wide loop if that helps?
For example, my menu is:
Tag 1, Tag 1, Tag 2
because some of the child pages share the same tag. It just needs to be:
Tag 1, Tag 2
Hm, setting the unique
parameter to true
like I did above should actually take care of that?
Or do you want to get tags of the current page plus the ones of the child page?
If you want to get all tags of all pages:
$tags = $site->children()->index()->visible()->pluck('tags', ',', true);
But I wouldn’t do that if you have many pages.
No the first code worked. I think I deleted a “,” comma before the ‘true’ that made it go odd. Thank you.
Final code:
<ul>
<?php
$tags = $page->children()->visible()->pluck('tags', ',', true);
foreach($tags as $tag):
?>
<li><a href="#"><?php echo $tag ?></a></li>
<?php endforeach ?>
</ul>
Just need to figure out a way of turning tags (with spaces) into hyphenated strings.
Thanks @texnixe
@texnixe again thank you. Pointed me in the right direction.
I gone with setting my blueprint tags to lower:
tags:
label: Tags
type: tags
lower: true
and using str_replace
so:
<ul>
<?php
$tags = $page->children()->visible()->pluck('tags', ',', true);
foreach($tags as $tag): ?>
<li><a href="#" class="<?php echo str_replace(' ','-',$tag); ?>"><?php echo $tag ?></a></li>
<?php endforeach ?>
</ul>
and used text-transform: capitalize;
for the menu
Oh, I see what you were trying to achieve. You might as well use str::slug()
.