List Blog Posts Confluence User Macro

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.

List Blog Posts

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:

Edit Blog Post Macro

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:

List Blog Post Macro Browser

Adding this macro to my Blog Summary Page it now looks like:

Blog Summary Page

Hope you find this useful. The source code is available on Bitbucket.

Related Links

Tips via Twitter for Confluence enterprise wiki

Got a Confluence tip? Tweet it now then see it in the Confluence docs.

6 thoughts on “List Blog Posts Confluence User Macro

  1. 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.

Leave a Reply