Skip Navigation

[Resolved] 2 lists of posts on the same page than combine posts of the same year under one

This support ticket is created 3 years, 10 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: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by peterZ 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#2110391

Tell us what you are trying to do?
I tried creating a list of upcoming and past events with 2 views and having the posts be grouped into their years, like they are on this page:
hidden link

Unfortunately the shortcode "heading" cannot distinguish between 2 lists on the same page, so the years that are in the list of upcoming events are missing in the list of past events.

Is there any documentation that you are following?
https://toolset.com/forums/topic/use-toolset-to-create-a-list-of-links-for-year-and-month-archive-pages-and-count/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2110855

Hello and thank you for contacting Toolset support.

This is most probably caused by the code of the heading shortcode. Somehow it does not handle this case(be used in two lists). Can you share the code of the shortcode in your next reply? Please wrap it inside <code> and </code> tags:


.... code here ....

#2110987

Hi Jamal,

it is exactly the same as in the other topic.

Here it is:

//group by month and year
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 '';
}
#2111001

I am sorry, but this is still not clear to me. Would you allow me temporary access to your website to check this closely? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

Can you also provide more details? What would you expect to have? Keep in mind that I work on several clients' sites per day, and I can't just guess what you want to achieve, your help is very needed.

#2112133

Indeed, the shortcode is not optimized to handle more than one view on the same page. It uses static variables, and they will always hold the latest value. We can overcome this by updating the code to handle multiple views. For example, using the global variable $WP_Views->current_view(), but that is not really simple, and will need extensive testing.

The easiest solution is to define another shortcode and use it on the 2nd view. I defined another shortcode "heading2" using another function and updated the 2nd view to use it. That produces the expected results.

add_shortcode('heading2', 'my_heading2');

function my_heading2($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 '';
}
[heading2 condition="year" value="[types field='startdatum' format='Y'][/types]"]
<h2>[types field='startdatum' format='Y'][/types]</h2>
[/heading2]

I hope this helps. Let me know if you have any questions.

#2113067

My issue is resolved now. Thank you!