php 在 Wordpress 中显示当前年份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20370582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:07:28  来源:igfitidea点击:

Display Current Year in Wordpress

phpwordpress

提问by Willem

I need to display the current year in Wordpress, now this is already being done in the footer template using php:

我需要在 Wordpress 中显示当前年份,现在这已经使用 php 在页脚模板中完成了:

<?php echo date('Y'); ?>

The site in question is for a car dealer. So there are many year model references in the page/post content ie.

有问题的网站是为汽车经销商准备的。所以页面/帖子内容中有很多年份的模型参考,即。

<h1>2013 Volkswagen Polo</h1>

All of these need updated on the first of January each year. However the php code doesn't work within the page/post content, only in template files. So I need a different way to do this.

所有这些都需要在每年一月一日更新。但是,php 代码在页面/帖子内容中不起作用,仅在模板文件中起作用。所以我需要一种不同的方式来做到这一点。

Any ideas?

有任何想法吗?

Thanks in advance
Willem

提前致谢
威廉

回答by Igor Evstratov

Why don't you create the simple shortcode function for this purpose?

为什么不为此目的创建简单的短代码功能?

function currentYear( $atts ){
    return date('Y');
}
add_shortcode( 'year', 'currentYear' );

Then you will be able to put [year] to anywhere in the content area.

然后您将能够将 [year] 放在内容区域的任何位置。

回答by Tsugaru

A little modification of previous answer which also works fine:

对先前答案的一些修改也可以正常工作:

in functions.php:

在functions.php中:

<?php
    function currentYear(){
        return date('Y');
    }
?>

then in footer.php (or anywhere as mentioned before):

然后在footer.php(或前面提到的任何地方):

<p>&copy; 2016-<?php echo currentYear(); ?> <a href="#">your_homepage_title</a></p>

回答by Neeraj Kumar

There is a plugin in wordpress which enables php code inside your post or content which will render perfectly and give you resul. search for exec php plugin in wordpress and add into your wordpress.

wordpress 中有一个插件可以在您的帖子或内容中启用 php 代码,它可以完美呈现并为您提供结果。在wordpress中搜索exec php插件并添加到你的wordpress中。