Fetching Tags Problem

Hi,
I’ve been working on some kind of blog. I’ve built almost everything with the documentations but somehow I can’t get the tag filterBy function to work, as documented in the docs.

This is how I fetch the articles according to the blog solution. I added the counter at the beginning for a script.

<?php $i = 0; foreach($pages->children()->visible()->flip() as $buch): ?>

How to do combine this with that:

// fetch the basic set of articles
$articles = $page->children()->visible()->flip();

// add the tag filter
if($tag = param('tag')) {
   $articles = $articles->filterBy('tags', $tag, ',');
}

If tried all sorts of combinations but for know I couldn’t get it to work…

Happy to hear your solutions

Just use an if-else statement:

<?php
if($tag = param('tag')) {
   $articles = $page->children()->visible()->filterBy('tags', $tag, ',')->flip();
}
else {
  $articles = $page->children()->visible()->flip();
}

Thanks for the hint.

I’ve got I to work like it’s presented in the documentation. I’m working with the controller method.

I’m facing the problem that some of my »tags« are two words separated by spaces. I’ve read that there is the
urldecode( ). I couldn’t get that to work with the following function:

if ($sammlung = param('sammlung')) {
$articles = $articles->filterBy('sammlung', $sammlung, ',');
};

urlencode() the links to your tags and then urldecode() in the filterBy() function:

<a href="<?php echo $page->url() . '/sammlung:' . urlencode($sammlung) ?>">
if ($sammlung = param('sammlung')) {
$articles = $articles->filterBy('sammlung', urldecode($sammlung), ',');
};
2 Likes

That worked out well! Thank you very much.

But know I’m getting to the next problem:

  1. One filed is input for authors. If there are listed two authors, the link is generated for both authors. Is there a way to generate two separate links for each author divide by a comma?
    A workaround would be two have two separate input fields in the backend, but is there a programmed solution?

  2. Although the values are en- and decoded German umlauts are somehow not correctly interpreted, so that the generated link won’t finde any entry.

Sure:

foreach(explode(',', $page->authors()) as $author):
  // Remove whitespace around the name
  $author = trim($author);
?>
  <a href="<?php echo $page->url() . '/author:' . urlencode($author) ?>">
<?php endforeach ?>

Or the Kirby way:

foreach ($page->authors()->split() as $author) {
//do stuff
}
1 Like

Hmm tried to implement it, but it failed with either of them :

<span class="buch-uav"><span class="discription">AUTOR</span>
    <a href="<?php echo $page->url() . '/autor:' . urlencode($article->autor()->html()) ?>">
    <?php echo $article->autor()->html() ?></a> 			
</span>

Could someone be so nice and help me.
Thank you very much in advanced

What exactly does not work? Do you still get wrong urls? Maybe you could post your complete code, i.e. controller and template?

By using $article->autor()->html(), you are again using the contents of the whole field. Try using $author instead.

Something wrong, I tried several possibilities yesterday but I didn’t work.
I’m a bit embarrassed to post my code here, I’ve just learned all php by using kirby. It’s somehow glued together by all the tutorials in the documentation. I guess there are plenty ways to improve my code. Please feel free to comment :blush:
Thank you in advance

This is the template:

<section id="buch">
	<div id="Container" class="container">
		<ul>
		<?php $i = 0; foreach($articles as $article): ?>
		<li>
		<div class="flip mix <?php echo $article->art()->html() ?> <?php echo $article->kategorie()->html() ?> <?php echo $article->thema()->html() ?>"> 
			<div class="buch-datum"><time datetime="<?php echo $article->date('c', 'date') ?>"><?php echo $article->date('ymd', 'date') ?></time></div>
			<div class="buch-title"><a name="<?php echo $article->title()->html() ?>"><?php echo $article->title()->html() ?></a></div>
		</div>
		<div class="panel">
			<div class="slidescontainer">
			<div class="slidesjs" id="slides-<?= $i ?>">
				<?php foreach($article->images() as $image): ?>
				<img src="<?php echo $image->url() ?>">
				<?php endforeach ?>
			</div>
		</div>
		<div class="buch-wrapper">
			<span class="buch-uav"><span class="discription">UNTERTITEL</span><?php echo $article->untertitel()->html() ?></span>		
			<span class="buch-uav"><span class="discription">AUTOR</span>
				<a href="<?php echo $page->url() . '/autor:' . urlencode($article->autor()->html()) ?>"><?php echo $article->autor()->html() ?></a> 
				<?php echo $article->autor2()->html() ?>
			</span>
			<span class="buch-uav"><span class="discription">VERLAG</span>
				<a href="<?php echo $page->url() . '/verlag:' . urlencode($article->verlag()->html()) ?>"><?php echo $article->verlag()->html() ?></a>
			</span> 
			<span class="buch-sprache"><span class="discription">SPRACHE</span><?php echo $article->sprache() ?></span>
			<span class="buch-auflage"><span class="discription">AUFLAGE</span><?php echo $article->auflage()->html() ?></span>					
			<span class="buch-jahr"><span class="discription">JAHR</span><?php echo $article->jahr()->hmtl() ?></span>	
			<span class="buch-shop"><span class="discription">SHOP</span><?php echo $article->shop()->html() ?></span>
			<span class="buch-land"><span class="discription">LAND</span><?php echo $article->land()->html() ?></span>
			<span class="buch-preis"><span class="discription">PREIS</span><?php echo $article->preis()->html() ?></span>
			<span class="buch-gebraucht"><span class="discription">GEBRAUCHT</span>
				<?php if($article->gebraucht() == '1'): ?><span>✔</span><?php else: ?><span>—</span><?php endif ?>
			</span>
			<span class="buch-geschenk"><span class="discription">GESCHENK</span>
				<?php if($article->geschenk() == '1'): ?><span>✔</span><?php else: ?><span>—</span><?php endif ?>
			</span>
			<span class="buch-text"><span class="discription">INFO</span><?php echo $article->text()->html() ?></span>
			</div>
		<div class="buch-wrapper2">
			<span class="buch-art"><span class="discription">ART</span>
				<a href="<?php echo $page->url() . '/art:' . urlencode($article->art()->html()) ?>"><?php echo $article->art()->html() ?></a>
			</span>
			<span class="buch-kategorie"><span class="discription">KATEGORIE</span>
				<a href="<?php echo $page->url() . '/kategorie:' . urlencode($article->kategorie()->html()) ?>"><?php echo $article->kategorie()->html() ?></a>
			</span>
			<span class="buch-form"><span class="discription">FORM</span>
				<a href="<?php echo $page->url() . '/form:' . urlencode($article->form()->html()) ?>"><?php echo $article->form()->html() ?></a>
			</span>
			<span class="buch-thema"><span class="discription">THEMA</span>
				<a href="<?php echo $page->url() . '/thema:' . urlencode($article->thema()->html()) ?>"><?php echo $article->thema()->html() ?></a>
			</span>
			<span class="buch-gelesen"><span class="discription">GELESEN</span>
				<?php if($article->gelesen() == '1'): ?><span>✔</span><?php else: ?><span>—</span><?php endif ?>
			</span>
			<span class="buch-nummer"><span class="discription">ID</span>
				<time datetime="<?php echo $article->date('c', 'date') ?>"><?php echo $article->date('ymd', 'date') ?></time><?php echo $article->nummer()->html() ?>
			</span><span class="buch-typeface"><span class="discription">TYPEFACE</span>
				<a href="<?php echo $page->url() . '/typeface:' . urlencode($article->typeface()->html()) ?>"><?php echo $article->typeface()->html() ?></a>
			</span>
			<span class="buch-gestaltung"><span class="discription">GESTALTUNG</span>
				<a href="<?php echo $page->url() . '/gestaltung:' . urlencode($article->gestaltung()->html()) ?>"><?php echo $article->gestaltung()->html() ?></a>
			</span>
			<span class="buch-sammlung"><span class="discription">SAMMLUNG</span>
				<a href="<?php echo $page->url() . '/sammlung:' . urlencode($article->sammlung()->html()) ?>"><?php echo $article->sammlung()->html() ?></a>
			</span>
			<span class="buch-link"><span class="discription">LINK</span>
				<?php if(!$article->links()->empty()): ?>
				<ul>
				<?php foreach($article->links()->pages() as $item): ?>
				<li>
				<a href="#<?php echo $item->title()->html() ?>"><?php echo $item->title()->html() ?></a>
				</li>
				<?php endforeach ?>
				</ul>
				<?php endif ?>
			</span>	
			</div>
		</li>
		<?php $i++; endforeach ?>
		</ul>
	</div>	
</section>

This is the controller

    <?php

return function($site, $pages, $page) {

  // fetch the basic set of pages
  $articles = $page->children()->visible()->flip();

  // add the tag filter
  if ($sammlung = param('sammlung')) {
  	$articles = $articles->filterBy('sammlung', urldecode($sammlung), ',');
	};
  if($verlag = param('verlag')) {
 $articles = $articles->filterBy('verlag', urldecode($verlag), ',');
  };
  if($typeface = param('typeface')) {
 $articles = $articles->filterBy('typeface', urldecode($typeface), ',');
  };
  if($gestaltung = param('gestaltung')) {
 $articles = $articles->filterBy('gestaltung', urldecode($gestaltung), ',');
  };
  if($autor = param('autor')) {
 $articles = $articles->filterBy('autor', urldecode($autor), ',');
  };
  if($art = param('art')) {
 $articles = $articles->filterBy('art', urldecode($art), ',');
  };
 if($kategorie = param('kategorie')) {
 $articles = $articles->filterBy('kategorie', urldecode($kategorie), ',');
  };
 if($form = param('form')) {
$articles = $articles->filterBy('form', urldecode($form), ',');
  };
 if($thema = param('thema')) {
$articles = $articles->filterBy('thema', urldecode($thema), ',');
  };
  // fetch all tags
  $tags = $articles->pluck('tags', ',', false);

   return compact('articles', 'tags', 'tag', 'sammlung', 'typeface', 'verlag', 'gestalung', 'kategorie', 'art', 'thema', 'form');

};