PHP 自定义简码正在附加单词“Array”。我该如何摆脱这个?

问题描述 投票:0回答:1

我有一个自定义的短代码,显示我的帖子的最后更新日期。短代码正在输出正确的日期,但附加了单词“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' );
php wordpress shortcode
1个回答
0
投票

您面临的问题可能是由于您在函数内初始化 $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”一词被附加到您的输出中。

© www.soinside.com 2019 - 2024. All rights reserved.