Skip Navigation

[Resolved] Group Views results by year and month – when two views are shown

This thread is resolved. Here is a description of the problem and solution.

Problem:

Using the group Views results by year and month code into multiple post views on same page.

Solution:

Please try to modify the PHP codes as below:

https://toolset.com/forums/topic/group-views-results-by-year-and-month-when-two-views-are-shown/#post-2180755

Relevant Documentation:

https://toolset.com/2013/10/how-to-group-views-results-by-year-and-month/

This support ticket is created 3 years, 3 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by bobA-2 3 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#2175917
screenshot.png
screenshot1.png

I am using the group Views results by year and month code described here https://toolset.com/2013/10/how-to-group-views-results-by-year-and-month/

If I use two different views on the same page that both use heading shortcode it seems sometimes the heading for the first item in the second view is not shown as seen in the uploaded image. This happens when the same date heading is the last shown in the previous view.

The heading works fine when the first view is not shown.

Any way to modify the code to make this not happen?

#2175941

I guess I could just make a second differently named duplicated group heading add_shortcode + function so they do not interact. Would that be would you would recommend or is there a better solution?

#2176835

Hello,

You are right, the [heading] shortcode is using static PHP variable, those static PHP variable will take effect on same page.

In your case, please try to modify the PHP codes as below:

add_shortcode('heading', 'my_heading');
  
function my_heading($atts, $content = '') {
	static $current_view_id = null;
	global $WP_Views;
	$view_id = $WP_Views->current_view;
	if($current_view_id != $view_id){
		unset($year, $month);
		$current_view_id = $view_id;
	}
	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 '';
}

And test again

#2177287

Hello and thanks for your reply.

I tried the modified code but it still only shows header for first view.

#2180755

Please try to modify the PHP codes as below:

function my_heading($atts, $content = '') {
	static $year = null;
	static $month = null;
	static $current_view_id = null;
	global $WP_Views;
	$view_id = $WP_Views->current_view;
	if($current_view_id != $view_id){
		$year = null;
		$month = null;
		$current_view_id = $view_id;
	}

	$condition = $atts['condition'];
	$value = $atts['value'];
	switch ($condition) {
		case 'year':
		case 'month':
		if ($$condition != $value) {
			$$condition = $value;
			return $content;
		}
		break;
	}
	return '';
}

And test again, I have tried it in my localhost it works fine.

#2180911

My issue is resolved now. Thank you!