简体   繁体   中英

Add WP permalink to PHP function

The code below adds an image into my 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

    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? Thanks

You can use the get_permalink function and pass it the post 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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