【问题标题】:If/ifelse and setting variables in Twig in Wordpress在 Wordpress 中的 Twig 中设置 if/ifelse 变量
【发布时间】:2020-05-10 00:26:30
【问题描述】:

我是 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>

【问题讨论】:

    标签: wordpress if-statement twig conditional-statements


    【解决方案1】:

    {% 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>

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多