简体   繁体   English

在return语句中运行echo语句

[英]Running an echo statement within a return statement

I'm trying to create a dynamic footer, where the year is automatically updated. 我正在尝试创建一个动态的页脚,该年份自动更新。 In PHP, this can be done using the following snippet: 在PHP中,可以使用以下代码段完成此操作:

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

My site is on Wordpress, and because of the way the footer is structured in the theme, I am inputting the copyright notice in place of the generic Wordpress "powered by" statement, using the following filter: 我的网站位于Wordpress上,由于页脚在主题中的结构方式不同,因此我使用以下过滤器输入版权声明,以代替通用的Wordpress“ powered by”语句。

<?php
add_filter ('esc_html', 'wpse_245817_esc_html', 100, 2 );
function wpse_245817_esc_html( $safe_text, $text ) {
    if ( $safe_text == 'Powered by %2$s' ) {
        return '&copy; Company';
    }
    return $safe_text;

This outputs the copyright symbol as well as the company name (©Company), but not the date, as I cannot place the PHP snippet directly within the return statement as is. 这将输出版权符号以及公司名称(©Company),但不会输出日期,因为我无法按原样将PHP代码段直接放置在return语句中。

What would be the appropriate way to add the snippet within the scope of the filter? 在过滤器范围内添加摘要的合适方法是什么? The final output I am trying to show is: ©2017 Company. 我想要显示的最终输出是:©2017 Company。

Try this, it will append the date to the return string. 试试这个,它将日期附加到返回字符串中。

if ( $safe_text == 'Powered by %2$s' ) {
    return '&copy; ' . date("Y") . ' Company';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM