简体   繁体   English

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

[英]Add WP permalink to PHP function

The code below adds an image into my wordpress RSS feed. 下面的代码将图像添加到我的wordpress RSS feed中。 I am wanting to make it so that the image is automatically hyperlinked back to the corresponding post. 我要制作它,以便图像自动超链接回相应的帖子。 The code is in my theme's functions.php 代码在我主题的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?

How can I wrap the get_the_post_thumbnail( $post->ID ) part of the code with a link? 如何用链接包装代码的get_the_post_thumbnail($ post-> ID)部分? Thanks 谢谢

You can use the get_permalink function and pass it the post ID. 您可以使用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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM