Forum

Thread tagged as: Problem, Blog

Categories in Blog Post List

Hello,

In the blog app, I like to display a list of the categories that correspond to each post in my post_in_list.html template. What I have used before that works is:

On my /blog/index.php page:

            $categories = perch_blog_post_categories(perch_get('s'),array(), true);
            PerchSystem::set_var('categorylist', $categories);

            perch_blog_custom(array(
                'each' => function($category) {
                    $category['categorylist'] = perch_blog_post_categories($category['postSlug'], array('template' => 'post_in_list_cats.html'), true);
                    return $category;
                },
                'template'      => 'post_in_list.html',
                'sort'          => 'postDateTime',
                'sort-order'    => 'DESC',
                'count'         => 8
            ));

Then somewhere in my post_in_list.html template:

<perch:if exists="categorylist">
     <perch:blog id="categorylist" encode="false" />
</perch:if>

To format the categories list, I use a post_in_list_cats.html template:

<perch:before>
   <ul class="list-inline cat-links">
</perch:before>
      <li><a href="archive.php?cat=<perch:blog id="categorySlug" />"><perch:blog id="categoryTitle" /></a></li>
<perch:after>
   </ul>
</perch:after>

Now, I'm trying to do the same in a new website in development, but it doesn't seem to work. My understanding from the recent perch categories updates, is that all I need to update is my post_in_list_cats.html template tags and id's:

<a href="archive.php?cat=<perch:blog id="categorySlug" />"><perch:blog id="categoryTitle" /></a>

to:

<a href="archive.php?cat=<perch:category id="catSlug" />"><perch:category id="catTitle" /></a>

Am I doing this right or is there something I'm missing, because even after this change, its still not working. Any help is greatly appreciated.

Thanks, Joshua

Joshua Rodriguez

Joshua Rodriguez 2 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That's right. What result do you get?

Hi Drew,

The following is a screenshot of the output I get in my browser. Next to the folder icon, the categories would display on previous projects that still are running Perch 2.5.4, but not my current project, on the latest version of Perch and the blog. Any ideas?

Blog Post Categories

The following is also a screen shot but of a previous project that is running Perch 2.6.3 and Perch Blog 4.1, the orange links appearing under the date are using the exact same code as previously mentioned. So I know the code should work, but I don't know if maybe I was missing something or misunderstanding something with regards to the Perch Categories updates of the most recent version of Perch and Blog.

Blog Post Categories

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think you need to do that. Just add the categories tag to your listing template - copy it from the post template.

Works great Drew. I just copied and dropped the Categories section from in the post.html template like you said, into the post_in_list.html template. No need for the post_in_list_Cats.html template or any additional code on my page. For reference purposes in case someone else wants to do the same, this is what worked for me.

On my page:

perch_blog_custom(array(
     'template'  => 'post_in_list.html',
     'sort'  => 'postDateTime',
     'sort-order'  => 'DESC',
     'count' => 8
));

And somewhere in my post_in_list.html template:

<ul class="list-inline cat-links">
     <perch:categories id="categories" set="blog">
         <li><a href="archive.php?cat=<perch:category id="catSlug" />"><perch:category id="catTitle" /></a></li> 
     </perch:categories>
</ul>

Thanks Drew