If / ifelse и установка переменных в Twig в Wordpress - PullRequest
0 голосов
/ 23 января 2020

Я новичок в Twig и у меня проблема с отображением даты в формате AP. Я устанавливаю переменную для даты месяца, и у меня есть оператор if, чтобы установить номер месяца для сокращения названия месяца этого месяца. Тем не менее, это не работает. Я полагаю, что мой синтаксис неправильный, потому что это длинный условный код. Любая помощь будет принята с благодарностью!

{% set show_month = show.get_field('start_date') | date('n') - 1 %}
{% set show_month_numerical = show.get_field('start_date') | date('m') %}
	{% if show_month_numerical == '01' %}
		{% set show_month_apstyle = 'Jan.' %}
	{% elseif show_month_numerical == '02' %}
		{% set show_month_apstyle = 'Feb.'%}
	{% elseif show_month_numerical == '08' %}
		{% set show_month_apstyle = 'Aug.'%}
	{% elseif show_month_numerical == '09' %}
		{%s set how_month_apstyle = 'Sept.'%}
	{% elseif show_month_numerical == '10' %}
		{% set show_month_apstyle = 'Oct.'%}
	{% elseif show_month_numerical == '11' %}
		{% set show_month_apstyle = 'Nov.'%}
	{% elseif show_month_numerical == '12' %}
		{%  set show_month_apstyle = 'Dec.'%}
	{% endif %}
  
  <h2 class="beta">           
  <span class="start-date-mdy">{{ show.get_field('start_date')|date('m j, Y') }}</span>
  </h2>

1 Ответ

0 голосов
/ 23 января 2020

{% set show_month = show.get_field('start_date') | date('n') - 1 %}
{% set show_month_numerical = show.get_field('start_date') | date('m') %}
	{% if show_month_numerical == '01' %}
		{% set show_month_apstyle = 'Jan.' %}
	{% elseif show_month_numerical == '02' %}
		{% set show_month_apstyle = 'Feb.'%}
	{% elseif show_month_numerical == '08' %}
		{% set show_month_apstyle = 'Aug.'%}
	{% elseif show_month_numerical == '09' %}
		{%s set how_month_apstyle = 'Sept.'%}
	{% elseif show_month_numerical == '10' %}
		{% set show_month_apstyle = 'Oct.'%}
	{% elseif show_month_numerical == '11' %}
		{% set show_month_apstyle = 'Nov.'%}
	{% elseif show_month_numerical == '12' %}
		{%  set show_month_apstyle = 'Dec.'%}
	{% endif %}
  
  <h2 class="beta">           
  <span class="start-date-mdy">{{ show_month_apstyle }}{{ show.get_field('start_date')|date('j, Y') }}</span>
			</h2>
...