Mango



Thread: Odd Question: Display posts from 2 categories on one page

Created on: 02/16/10 07:53 PM

New topic Reply    Page: 1  

Replies: 3
markmandel


markmandel's Gravatar
Joined: 09/29/09
Posts: 21

02/16/10 7:53 PM

This is going to sound weird ;o)

I want to link to a page on my mango blog that has a combination of posts from 2 separate categories.

(Yeah, pretty special case I know)

Is there a way that I can even do this? I'm going to assume that the underlying engine will probably only give me one category at a time?

Any advice would be great :D
Link | Top | Bottom
Laura

Wizard
Laura's Gravatar
Joined: 01/29/05
Posts: 1521

02/16/10 9:50 PM

Hi Mark,
There are a couple of ways doing this. You could create a custom template just for this and hardcode the 2 categories:
<Posts source="multicategory" categoryName="category-1,category-2">...</Posts>

The only issue with that is that the paging will probably not work.

You could also create your own Archive tag (ie: <custom:Archive>) and create a multi-category archive object, and then the posts will use that and the paging will work.

You could also do modify the request.archive variable with something like:
<cfset request.archive = request.blogManager.getArchivesManager().getArchive("multicategory","category-1,category-2")>, then wrap the <Posts> in an archive tag as normal.

The best way would be that the archives.cfm page handle the multiple categories like the rss feeds do (feeds/rss.cfm/category/category-1,category-2). I think that would be the best option, but it needs to be modified in the core.
Link | Top | Bottom
markmandel


markmandel's Gravatar
Joined: 09/29/09
Posts: 21

02/16/10 9:53 PM

That's awesome! Thanks for that Laura.

I'll hack at it, and see where I get to.
Link | Top | Bottom
markmandel


markmandel's Gravatar
Joined: 09/29/09
Posts: 21

03/07/10 11:18 PM

I got this to work, but I had to hack at some of the core code to get it to happen.

This is probably not the best way, but it worked ;o)

Inside /archives.cfm, I add the check for multiple categories:

<cfcase value="category">

   <cfif arraylen(request.externalData.raw) GTE 2>

      <cfset data.name = request.externalData.raw[2]>

      <cfset request.externalData.categoryName = request.externalData.raw[2] />

      <!--- are there pages? --->

      <cfif arraylen(request.externalData.raw) GTE 3 AND request.externalData.raw[3] EQ "page">

         <cfif arraylen(request.externalData.raw) GTE 4>

            <cfset request.currentPageNumber = request.externalData.raw[4]>

         </cfif>

      </cfif>



      <!--- check if there are multiple categories --->

      <cfif structkeyexists(data,"name") AND listlen(data.name) GT 1>

         <cfset archiveType = "multicategory" />

      </cfif>



   <cfelse>

      <!--- no category name, just show most recent --->

      <cfset archiveType = "recent" />

   </cfif>



</cfcase>


This worked, but pagination was failing. I worked out that inside /components/ArchiveManager.cfc, the 'archive' object being returned had no 'postCount' property.

This wasn't the most performant way of making this work, but it works ;)

<cfcase value="multicategory">

   <cfset archive = createObject("component","model.CategoryArchive") />

   <cfset archive.category = arguments.data.name />

   <cfset archive.postCount = 0>

   <!--- not the best way, but it will work --->

   <cfloop list="#archive.category#" index="category">

      <cfset archive.postCount += variables.appManager.getCategoriesManager().getCategoryByName(category).getPostCount()>

   </cfloop>

   <cfset archive.urlString = replacenocase(blog.getSetting("categoryUrl"),"{categoryName}",archive.category,"all") />

   <cfset archive.type = "multicategory">

</cfcase>


Now I can browse to:
http:/server/archives.cfm/category/photography,gift-ideas

And it shows me posts from the two categories :)

Thanks for your help!
Link | Top | Bottom

New Post

Please login to post a response.