网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

截断Wordpress上的日期对象

GG网络技术分享 2025-03-18 16:12 1


问题描述:

When my website shows a snippet of a blog post through any widget, it displays the featured image, the title, the first characters of the text itself and the date the post was made.

Inside Wordpress\' content.php, it shows the date using the_time(), like this:

<div class=\"date-box\">

<div class=\"day\"><?php the_time(\'d\'); ?></div>

<div class=\"month\"><?php the_time(\'M\'); ?></div>

</div>

The parameter \'M\' passed in the function the_time() is supposed to give PHP the month written with only 3 characters (January would be JAN, February would be FEB and so on).

In my native language (which is portuguese) it gets the months, obviously, in portuguese: January is Janeiro, February is Fevereiro... which doesn\'t matter for the_time(\'M\'), it truncates it the same way.

My problem is that the month May is Maio in portuguese, a 4-letter month. Upon passing it to PHP, for some obscure reason, it doesn\'t get truncated. It keeps it Maio, which obviously prejudices my layout.

I\'m not a PHP developer, but I did a little research and tried a workaround inside the content.php, but it doesn\'t work:

<div class=\"month\">

<?php

$t = the_time(\'M\');

if(strlen($t) > 3){

echo mb_strimwidth($t, 0, 3);

}else{

echo $t;

};

?></div>

It might even be syntax issues, because I really don\'t know PHP, but it seems that I might need it to solve this problem. Am I going the right way here? I appreciate any help you give me!

TL;DR: I need to truncate a 4-letter date into a 3-letter one and pass it back to HTML.

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

当我的网站通过任何小部件显示博客帖子的片段时,它会显示特色图片,标题, 文本本身的第一个字符和帖子的日期</ em>。</ p>

在Wordpress的content.php中,它使用 the_time()显示日期 </ em>,像这样:</ p>

 &lt; div class =“date-box”&gt; 

&lt; div class =“day”&gt;&lt;?php the_time(\'d\'); ?&gt;&lt; / div&gt;

&lt; div class =“month”&gt;&lt;?php the_time(\'M\'); ?&gt;&lt; / div&gt;

&lt; / div&gt;

</ code> </ pre>

函数the_time()中传递的参数\'M\'应该为PHP提供月份 只写3个字符(1月份是1月,2月份是FEB等等)。

用我的母语(葡萄牙语),很明显,用葡萄牙语来表示几个月:1月是 Janeiro </ em> ,二月是 Fevereiro </ em> ...这对于 the_time(\'M\')</ em>无关紧要,它会以同样的方式截断它。</ p>

我的问题是 五月份的葡萄牙语是 Maio </ em>,一个4个字母的月份。 在将其传递给PHP时,由于某些不明原因,它不会被截断</ em>。 它保留了 Maio </ em>,显然会损害我的布局。

我不是PHP开发人员,但我做了一些研究并在content.php中尝试了一种解决方法,但它不起作用:

 <  code>&lt; div class =“month”&gt; 

&lt;?php

$ t = the_time(\'M\');

if(strlen($ t)&gt; 3){

echo mb_strimwidth($ t ,0,3);

} else {

echo $ t;

};

?&gt;&lt; / div&gt;

</ code> </ pre>

它可能 甚至是语法问题,因为我真的不懂PHP,但似乎我可能需要它来解决这个问题。 我在这里走的是正确的吗? 感谢你给我的任何帮助!</ p>

TL; DR:我需要将4个字母的日期截断为3个字母的日期并将其传回HTML。</ p> \\ n </ div>

网友观点:

Wordpress\' the_*() functions all echo their output directly and don\'t return anything, so you can\'t assign the value to a variable. You\'re looking for get_the_time().

The rest of your code looks like it should work just fine.

标签:

提交需求或反馈

Demand feedback