我有一个自定义的短代码,显示我的帖子的最后更新日期。短代码正在输出正确的日期,但附加了单词“Array”?我该如何摆脱这个?
//Last Updated Date Blogs
function show_last_updated( $content ) {
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
$updated_date = get_the_modified_time('m/d/Y');
$updated_time = get_the_modified_time('h:i a');
$custom_content .= '<p class="last-updated-date">Updated: '.$updated_date.'</p>';
}
$custom_content .= $content;
return $custom_content;
}
add_shortcode( 'the_content', 'show_last_updated' );
您面临的问题可能是由于您在函数内初始化 $custom_content 变量的方式所致。由于您在初始化之前使用它,PHP 将其视为数组。要解决此问题,您应该在使用 $custom_content 之前对其进行初始化。这是更正后的代码:
php 复制代码
// Last Updated Date Blogs
function show_last_updated($content) {
$custom_content = ''; // Initialize $custom_content
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
$updated_date = get_the_modified_time('m/d/Y');
$updated_time = get_the_modified_time('h:i a');
$custom_content .= '<p class="last-updated-date">Updated: '.$updated_date.'</p>';
}
$custom_content .= $content;
return $custom_content;
}
add_shortcode('the_content', 'show_last_updated');
此更改应该可以防止“Array”一词被附加到您的输出中。