• I’m trying to find a way to produce a list of posts, grouped as individual months, and with a post-count for the posts-per-month.

    ——————
    The format is this, in which x is the post count for that month:

    December 2016 [x]
    Post 1 title.
    Post 2 title.

    November 2016 [x]
    Post 1 title.
    Post 2 title.
    ——————

    I can get the list/format- but don’t know how to include a count.

    I haven’t included (but will if required) my current code, because there’s various versions of it and I don’t know if it’s a sensible basis – so rather than try to improve something which may be flawed, I’m open to fresh suggestions.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Since posts are generally listed in chronological order, you already have the proper listing, it’s just the month labels and counts are missing. You can start with the standard Loop and add code that detects when the current post’s month changes from the previous post’s. This can be done through one of the PHP date formatting functions.

    If you just needed a month label, that would be it. Just output the label and wait until the month changes again. Because you want a count that needs to be output before all the posts for the month are output, it gets more complicated. You need to make use of PHP’s output buffering so that output is not immediately sent to the browser, but placed into a buffer.

    When you output the month label after starting the buffer, include a placeholder string for the count that is very unlikely to occur naturally in the buffered output. Something like %count% for example. Keep a running tally of posts that are output to the buffer. When the month changes again, collect the buffer contents into a variable and do a search/replace for that placeholder and insert the actual count in its place. Now the variable with the buffer content and count value can be output to the browser and the process restarted for the next month.

    Thread Starter gulliver

    (@gulliver)

    @bcworkz… thanks. I think perhaps I’ve posted in the wrong forum – because i don’t know enough to be able to act on your suggestion.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Archive with post count’ is closed to new replies.