Skip Navigation

[Resuelto] Openin hours with weekly rotation

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I would like to alternate the display of two Views depending on whether the current week of the year is odd or even.

Solution:
Use the modulus operator and PHP's date functions to determine whether the current week is odd or even. Then register the shortcode in 3rd-party shortcode arguments and use it in a conditional:

add_shortcode( 'ts-even-odd-week', 'ts_even_odd_week_func');
function ts_even_odd_week_func( $atts, $content ) {
  $week_number = strftime('%U');
  $is_odd = ($week_number % 2 == 1);
  return $is_odd ? 1 : 0;
}
[wpv-conditional if="( '[ts-even-odd-week]' eq '1' )"]
This is an odd week - insert the odd View here.
[/wpv-conditional]
[wpv-conditional if="( '[ts-even-odd-week]' eq '0' )"]
This is an even week - insert the even View here.
[/wpv-conditional]
This support ticket is created hace 6 años. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Etiquetado: ,

Este tema contiene 3 respuestas, tiene 2 mensajes.

Última actualización por Altti Papinsaari hace 6 años.

Asistido por: Christian Cox.

Autor
Mensajes
#1147951

Our customer (pharmacy) has different opening hours on odd and even weeks. I have made a post type "Opening hours" and there is two posts: "Odd weeks" and "Even weeks". I need a view that rotates those posts on weekly basis. How can I accomplish this?

What is the link to your site?
enlace oculto

#1149973

Hi, sorry for the delay responding to your request. We had a problem here in the forum and your ticket was not assigned to a supporter correctly. I will try to help. There is nothing built in to Toolset that will help you schedule visibility of content like this by week. We do offer conditional HTML structures and give you the ability to add your own custom conditional logic. Here's how it could work.

Using PHP's strftime function you can get a numeric representation of the current week of the year:

$week_number = strftime(%U); // Week number of the given year, starting with the first Sunday as the first week

You can use the modulus operator to determine if that number is odd or even:

$is_odd = ( $week_number % 2 == 1 );

So you probably need a custom shortcode that returns a 1 or a 0 depending on whether or not the current week is odd. Something like this:

add_shortcode( 'ts-even-odd-week', 'ts_even_odd_week_func');
function ts_even_odd_week_func( $atts, $content ) {
  $week_number = strftime('%U');
  $is_odd = ($week_number % 2 == 1);
  return $is_odd ? 1 : 0;
}

Add that code to your child theme's functions.php file, or create a new custom code snippet in Toolset > Settings > Custom Code. Then go to Toolset > Settings > Frontend Content and enter ts-even-odd-week in third-party shortcode arguments. Now you can use this shortcode in conditional HTML to determine which View should be displayed:

[wpv-conditional if="( '[ts-even-odd-week]' eq '1' )"]
This is an odd week - insert the odd View here.
[/wpv-conditional]
[wpv-conditional if="( '[ts-even-odd-week]' eq '0' )"]
This is an even week - insert the even View here.
[/wpv-conditional]

This is a very simple example that may need to be adjusted for your site's requirements.

#1150451

Thanks for help Christian. It didn't quite work straight like that. Had to do it a bit differently. I made a view for each post and added this to functions.php:

function openinghours_func() {
if (date('W')%2==1) {
echo do_shortcode( '[wpv-view name="openinghours-odd-weeks"]');
} else {
echo do_shortcode( '[wpv-view name="openinghours-even-weeks"]');
}
}
add_shortcode( 'openinghours', 'openinghours_func');

Now shortcode [openinghours] gives me the correct times.

#1150452

My issue is resolved now. Thank you!