Following on from my previous post on Creating a Blog Summary Page in Confluence here is a simple user macro which will list all of the blog posts in the current Confluence Space which were posted in the current month and the previous month, grouped by month.
To create the user macro:
1. Login to Confluence as a Confluence Administrator
2. Select Browse -> Confluence Admin
3. Select User Macros -> Create a new user macro
4. Enter the information as provided below:
Template:
## Macro title: List Blog Posts ## Macro has a body: Y or N (N) ## Body processing: Selected body processing option (No macro body) ## Output: Selected output option ## ## Developed by: Andrew Frayling ## Date created: 13/04/2012 ## Installed by: <your name> ## Macro to display blog posts for the current and previous months ## @noparams #set ( $allBlogsInSpace = $pageManager.getBlogPosts($space, true) ) ## get a calendar object for the current date #set ( $currentDate = $action.dateFormatter.getCalendar() ) ## Define an array of Month names #set ( $monthArray = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] ) ## set the current month #set ( $currentMonthNum = $currentDate.get(2) ) #set ( $currentMonth = $monthArray.get($currentMonthNum) ) ## Display posts for the current month <h5>$currentMonth</h5> #foreach($blog in $allBlogsInSpace) #if ($blog.getPostingMonth() == $currentMonth) #contentLink2($blog, true, false) $blog.getPostingDayOfMonth() $blog.getPostingMonth() $blog.getPostingYear()<br /> #end #end ## set the previous month #set ( $previousMonthNum = $currentDate.get(2) - 1 ) #set ($previousMonth = $monthArray.get($previousMonthNum) ) ## Display posts for the previous month <h5>$previousMonth</h5> #foreach($blog in $allBlogsInSpace) #if ($blog.getPostingMonth() == $previousMonth) #contentLink2($blog, true, false) $blog.getPostingDayOfMonth() $blog.getPostingMonth() $blog.getPostingYear()<br /> #end #end
5. Click Save
You can then use the macro on a page by using:
{listblogposts}
Or select the macro from the Macro Browser via Insert -> Other Macros:
Adding this macro to my Blog Summary Page it now looks like:
Hope you find this useful. The source code is available on Bitbucket.
Related Links
- Creating a Blog Summary Page in Confluence
- Confluence User Macros
- List Blog Posts Confluence User Macro source code on Bitbucket
Got a Confluence tip? Tweet it now then see it in the Confluence docs.
Is it possible to list only posts with certain labels?
Hi,
You should be able to use $blog.getLabels() to return a list of labels for a blog post and then loop through the list to see if the post contains the label you are looking for.
Have a look at the API docs at http://docs.atlassian.com/confluence/latest/com/atlassian/confluence/pages/PageManager.html#getBlogPosts(com.atlassian.confluence.spaces.Space, boolean) for the methods available for returning labels associated with a blog post.
Hope that helps?
Andrew.
I’ll try it out, thanks!
Great Macro, would there be a way to order the dates descending?
Does anyone have an answer to this?
Hi I tried this but I had to change the previous month as follows:
## set the previous month
#set ( $previousMonthNum = $currentDate.get(2) – 1 )
#if ( $previousMonthNum == -1)
#set ($previousMonthNum = 11)
#end
#set ($previousMonth = $monthArray.get($previousMonthNum) )
Otherwise each January you will have a value of -1 as the previousmonthnum rendering to an error until february.
Thanks for the code.