【问题标题】:WordPress featured image url in RSS feed template fileRSS 提要模板文件中的 WordPress 特色图片网址
【发布时间】:2021-11-01 17:27:45
【问题描述】:

我创建了一个自定义 RSS 提要模板文件:

<?php
/**
 * RSS 0.92 Feed Template for displaying RSS 0.92 Posts feed.
 *
 * @package WordPress
 */

header( 'Content-Type: ' . feed_content_type( 'rss' ) . '; charset=' . get_option( 'blog_charset' ), true );
$more = 1;

echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>

<articles>
<?php
while ( have_posts() ) :
    the_post();
    ?>
    <article>
        <title><?php the_title_rss(); ?></title>
        <teaser><?php the_excerpt_rss(); ?></teaser>
        <description><?php $original = strip_tags(get_the_content_feed('rss2'));
        $stripped = str_replace("&nbsp;", " ", $original);
        echo $stripped; ?></description>
        <link><?php the_permalink_rss(); ?></link>
        <image>http://placehold.it/100x100</image>
        <date><?php 
    echo mysql2date(
        'd-m-Y H:i:s', 
        get_post_time('Y-m-d H:i:s', true), 
        false
    ); 
?></date>
        <?php
        /**
         * Fires at the end of each RSS feed item.
         *
         * @since 2.0.0
         */
        do_action( 'rss_item' );
        ?>
    </article>
<?php endwhile; ?>
</articles>

我有一个问题,我希望它输出特色图片 url 但 atm。它只是用占位符图像硬编码。

我尝试过使用 get_the_post_thumbnail URL,如下所示:

<image><?php echo get_the_post_thumbnail_url('full'); ?></image>

但是回声只是空白。

我也在下面尝试过,但它只是回显数组。

<image><?php echo wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?></image>

如何在我的 RSS 提要中回显精选图片 URL?

【问题讨论】:

  • @Ruvee 是的......谢谢!它与在 get_the_post_thumbnail_url 中传递 id 完美配合 :)

标签: php wordpress image wordpress-theming rss


【解决方案1】:

如果您想使用get_the_post_thumbnail_url,请将 id 传递给它,如下所示:

<image>
   <url><?php echo get_the_post_thumbnail_url(get_the_ID(), 'full'); ?></url>
</image>

或者如果你想使用wp_get_attachment_image_url,那么你可以这样做:

<image>
   <url><?php echo wp_get_attachment_image_url(get_the_ID(), 'full'); ?></url>
</image>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多