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?
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?
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
Hello and thanks for your reply.
I tried the modified code but it still only shows header for first view.
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.
My issue is resolved now. Thank you!