Forum

Thread tagged as: Blog

How do I output each post's tags and categories in the post_in_list.html templat...

On the main blog list, alongside the post's title, link, an excerpt, the date it was posted and the author, I'd also like to output the categories it belongs to and any tags it's been given. A bit like this site: https://www.teehanlax.com/blog/

I've tried playing around with <perch:blog id="tagSlug" /> and <perch:blog id="tagTitle" /> but it doesn't work… Can't use php since the template files are html, so <?php perch_blog_tags(); ?> and <?php perch_blog_categories(); ?> are out.

Thanks for taking a look :)

Martin.

Martin Underhill

Martin Underhill 5 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use the each option

perch_blog_custom(array(
   'each' => function($item) {
      $item['categories'] = perch_blog_post_categories($item['postID']);
      return $item;
   }
));

Then in your template

<perch:blog id="categories" encode="false" />

Thanks Drew. How do I include my template in the array?

This doesn't work

<?php perch_blog_custom(array(
  'template' => 'special_listing.html',
  'each' => function($item) {
    $item['categories'] = perch_blog_post_categories($item['postID']);
    return $item;
    }
  ));
?>

By the way, I didn't get an email notification for your reply and can't find anything to turn them on…

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can set the options on perch_blog_post_categories() in the usual way:

<?php perch_blog_custom(array(
  'template' => 'special_listing.html',
  'each' => function($item) {
    $item['categories'] = perch_blog_post_categories($item['postID'], array(
                            'template'=>'foo.html'
                        ));
    return $item;
    }
  ));
?>

Email notifications are being worked on.

I've got this now:

<?php perch_blog_custom(array(
  'template' => 'post_in_list.html',
  'each' => function($item) {
    $item['categories'] = perch_blog_post_categories($item['postID'], array(
      'template'=>'category_only.html'
    ));
    return $item;
    }
  ));
?>

I've got this in my post_in_list.html template:

<p>
  Written by <a href="/about">Pete</a> on <span class="entry-published date"><perch:blog id="postDateTime" format="%d %B, %Y" /> in <perch:blog id="categories" encode="false" /></span>
</p>

and the category_only.html template looks like this:

<a href="/blog/category/<perch:blog id="categorySlug" />"><perch:blog id="categoryTitle" /></a>

The categories aren't appearing on the page. I'm doing something daft, aren't I…? Can you see what I'm doing? Sorry :\

Drew McLellan

Drew McLellan 2638 points
Perch Support

Oh, you need to return it:

$item['categories'] = perch_blog_post_categories($item['postID'], array(
      'template'=>'category_only.html'
    ), true);

Thanks very much! It's working now (sort of…):

<?php perch_blog_custom(array(
  'template' => 'post_in_list.html',
  'each' => function($item) {
    $item['categories'] = perch_blog_post_categories($item['postID'], array(
      'template'=>'category_only.html'
    ), true);
    return $item;
    }
  ));
?>

If I set the count to 1 it tells me it's 'page 1 of 1', rather than 'page 1 of 2' (there are 2 posts at the minute, while I'm building the blog). Adding 'pagination' => true, to the array doesn't do anything either, but I'm assuming that's because it thinks there's only one page. Here's what I'm doing:

<?php perch_blog_custom(array(
  'template' => 'post_in_list.html',
  'each' => function($item) {
    $item['categories'] = perch_blog_post_categories($item['postID'], array(
      'template'=>'category_only.html'
    ), true);
    return $item;
    },
  'paginate' => true,
  'count' => 1,
  ));
?>

Also, if there's more than one category returned for a given post, is there any way to pop a comma and a space after all but the last category using PHP magic? I could do it with CSS but it feels like something that should come from the back end.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you confirm you're on Blog 4.1?

Nope– just checked my RSS (haha– I'm one of the 'special' few who still uses it!) and noticed yesterday's releases! I'll get everything updated and see if it fixes things. Here's my current setup:

Perch: 2.4.9 Production mode: Development (10) Installed apps: content (2.4.9), perch_forms (1.7), perch_blog (4.0.3), perch_backup (1.2)

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great. That pagination issue should be fixed in 4.1.

Works a treat! Thanks :)

Do you know how I might tackle the space and comma issue with the back-end? I'm using perch:every to output a space and comma after every item, but is there a way to override the last item with no content? The only CSS-style attribute that seems works is nth-child, so I can't put a last-child in there…

<perch:every count="1">, </perch:every>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Using perch:every doesn't make sense if with count="1" - its unnecessary. You should be able to just do this:

<perch:if exists="perch_item_last"><perch:else />, </perch:if>

Thanks– works nicely! Didn't realise there was a perch_item_last :)

Final part of this puzzle: do you know how how I'd get the 'each' => function($item) code into my archive.php page? The code's a bit more complex there…

Worst case scenario, I could just use a different template that doesn't have the category in it, but it would be cool if it mirrored my blog/index.php page.

Thanks again for all your help.

(The markdown support in the new forum is great, by the way!)

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you using the example archive page, or something custom? You should just be able to pop it into the options before the perch_blog_custom() call.

Makes sense– sorry! I've done it now and it's working nicely. Thanks again for all of your help :)