将WP永久链接添加到PHP函数

时间:2016-08-17 00:26:51

标签: php wordpress function rss

下面的代码将图像添加到我的wordpress RSS提要中。我想这样做,以便图像自动超链接回相应的帖子。代码在我的主题的functions.php

    function wcs_post_thumbnails_in_feeds( $content ) {
    global $post;
    if( has_post_thumbnail( $post->ID ) ) {
        $content = get_the_post_thumbnail( $post->ID ) . '<span class="text">' . $content . '</span>';
    }
    return $content;
}
add_filter( 'the_excerpt_rss', 'wcs_post_thumbnails_in_feeds' );
add_filter( 'the_content_feed', 'wcs_post_thumbnails_in_feeds' );
Can I change this so that the post_thumbnail is automatically wrapped with a link to the post?

如何使用链接包装 get_the_post_thumbnail($ post-&gt; ID) 部分代码?感谢

1 个答案:

答案 0 :(得分:0)

您可以使用get_permalink函数并将其传递给帖子ID。

function wcs_post_thumbnails_in_feeds( $content ) {
        global $post;
        if( has_post_thumbnail( $post->ID ) ) {
            $content = '<a href="' . get_permalink( $post->ID ) . '">' . get_the_post_thumbnail( $post->ID ) . '</a><span class="text">' . $content . '</span>';
        }
        return $content;
    }