Skip Navigation

[Resolved] Grouping posts by year/month of publish date

This support ticket is created 9 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 6 replies, has 2 voices.

Last updated by Susannah 9 years, 2 months ago.

Assigned support staff: Ana.

Author
Posts
#140935
#141143

Ana
Supporter

Dear sussieT,

can you explain what you want to achieve?

Thanks,
Ana

#141224

Hi Ana, thanks for replying.

To show a "multidimensional?" list of my cpt News with first the year (if has posts) and then the month (if has posts).
I would love if it could be generated without the use of taxonomies, because that would be an extra thing for my admins to learn. And if they miss to use it for one post, it would not show up on the page or show up wrong.

It wish to generate an output like this:

2013

September
- <post title>
- <post title>
- <post title>
- <post title>

July
- <post title>
- <post title>
- <post title>
- <post title>

February
- <post title>
- <post title>
- <post title>

2012

December
- <post title>
- <post title>

May
- <post title>
- <post title>

I would like to create it as a view and not a wordpress archive. I think the wordpress archive shows a lot of default stuff that I cant control (comming from the theme template i guess), but I would like to start clean and be flexible. I would put my view in the content area of a News overview. And then put one in a widget area of the single pages of News.

Thank you.

#141242

Ana
Supporter

Dear sussieT,

this is not out of the box yet with views, however you can do the following:

1. create a view that will list all your News custom post type and order by post date descending.
2. insert in the Layout HTML in the loop the following custom short codes:

    [heading condition="year" value="[wpv-post-date format="Y"]"]
    <h4>[wpv-post-date format="Y"]</h4>
    [/heading]
    [heading condition="month" value="[wpv-post-date format="F"]"]
    <h5>[wpv-post-date format="F"]</h5>
    [/heading]

3. insert in your theme functions.php file the following code:

add_shortcode('heading', 'my_heading');
function my_heading($atts, $content = '') {
  static $year = null;
  static $month = null;
  $condition = $atts['condition'];
  $value = $atts['value'];
  switch ($condition) {
	  case 'year':
	  case 'month':
		if ($$condition != $value) {
			$$condition = $value;
			return $content;
		}
	  	break;
  }
  return '';
}

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Ana

#144332

It works, but the posts is missing.

#144349

Ana
Supporter

Dear sussieT,

sorry I didn't pasted the entire code of the views loop so here goes. This is the layout HTML content of the view:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
    [heading condition="year" value="[wpv-post-date format="Y"]"]
    <h4>[wpv-post-date format="Y"]</h4>
    [/heading]
    [heading condition="month" value="[wpv-post-date format="F"]"]
    <h5>[wpv-post-date format="F"]</h5>
    [/heading]
    <h4>[wpv-post-title]</h4>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		[wpml-string context="wpv-views"]<strong>No items found</strong>[/wpml-string]
	[/wpv-no-items-found]
[wpv-layout-end]

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Ana

#144389

It worked. Thank you.