【问题标题】:Time shifting with imported RSS feed into Wordpress将 RSS 提要导入 Wordpress 进行时移
【发布时间】:2016-10-19 20:53:21
【问题描述】:

我正在尝试设置一种将 rss 提要引入我设计的 Wordpress 主题的简单方法。我使用 'fetch_feed' 让它工作得非常好——所有的 rss 项目都通过了,我可以对它们进行排序并按照我的需要显示它们。

我遇到的问题是时移。我从中提取的提要位于澳大利亚服务器上,并且实际提要日期是正确的。客户端服务器,但是我不知道它在哪里,但是我按照托管公司的指示在 php.ini 文件中设置了时区,但是所有的 rss 提要项目都移动了 13 小时。

这是我获取 rss 提要的代码:

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'http://xxxxxxxx.com.au/events/feed/' );

if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

// Figure out how many total items there are, but limit it to 25. 
$maxitems = $rss->get_item_quantity( 25 ); 

// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );


endif;
?>

这是我在主题中用于显示提要的代码:

<?php if ( $maxitems == 0 ) : ?>
    <h3><?php _e( 'No items', 'my-text-domain' ); ?></h3>
<?php else : ?>
    <?php // Loop through each feed item and display each item as a hyperlink. ?>
    <?php foreach ( $rss_items as $item ) : ?>
    <p><b>
    <a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank"
                title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date() ); ?>">
                <?php echo esc_html( $item->get_title() ); ?>
       </a></b>
       <?php echo esc_html( $item->get_date('| j F | g:i a') ); ?><br>
       <?php echo sanitize_text_field( $item->get_content() ); ?>
        </p>
    <?php endforeach; ?>
<?php endif; ?>

非常感谢任何帮助...

【问题讨论】:

  • $item->get_date() 在没有格式化参数的情况下返回什么(如果有的话)(换句话说,什么样的默认值)?您也许可以获取此值并使用 strtotime() 或类似的东西来处理它...
  • 这是一个好问题 - 我删除了该行并刷新了页面,但没有任何改变。这可能很明显,但我不精通 PHP,所以我提前为我的无知道歉。我会尝试您对 srttotime 选项的建议,谢谢!

标签: php wordpress time rss


【解决方案1】:

谢谢 Kevin_Kinsey,我尝试了 strtotime,但即使这并没有奏效——这确实让我找到了答案!在所有的事情中,添加一个简单的 PHP 行

<?php
date_default_timezone_set("Australia/Sydney");
?>

修好了!现在所有日期都在适当的时间出现。再次感谢您的回复和愿意帮助凯文的意愿!

【讨论】:

  • 不客气。您的解决方案感觉是正确的……尤其是如果您在澳大利亚并且您的读者希望看到澳大利亚的时间戳。我想一个“圣杯”甚至可能是允许他们从下拉列表中选择他们的 TZ,设置一个 cookie,然后让你的插件读取它来设置 TZ,但我们可以把它留到以后。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多