Feed customization Wordpress

时间:2017-07-10 15:16:59

标签: wordpress

I am retrieving latests post rss from my blog using ?feed=rss2 parameter. I noticed that in the xml structure is missing the featured link image, (the image shown on the blog related to the post).Is there a way to include it?

2 个答案:

答案 0 :(得分:0)

请参阅下面的详细功能

function rss_post( $atts ) {
        //Thirs party website URL
        $client_url = "{'url']}";
        //gets count of post
        $count      = "{$atts['count']}";
        //return var
        $return_val = '';

            //replace with your URL
            $rss = fetch_feed("$client_url");

            if (!is_wp_error($rss)) :
                $maxitems = $rss -> get_item_quantity($count); 
                $rss_items = $rss -> get_items(0, $maxitems);
            endif;

            //grabs our post thumbnail image
            function get_first_image_url($html) {
                if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
                    return $matches[1];
                }
            }

            //shortens description
            function shorten($string, $length) {
                $suffix = '&hellip;';
                $short_desc = trim(str_replace(array("\r", "\n", "\t"), ' ', strip_tags($string)));
                $desc = trim(substr($short_desc, 0, $length));
                $lastchar = substr($desc, -1, 1);
                    if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?')
                            $suffix = '';
                            $desc .= $suffix;
                        return $desc;
                    }

                        //start of displaying our feeds
                        if ($maxitems == 0) echo '<li>No items.</li>';
                        else foreach ( $rss_items as $item ) :

                        $return_val .= '<div class="four columns omega">';

                            $return_val .= '<div class="pic">';
                                    $return_val .= '<img width="460" height="290" src="'. get_first_image_url($item -> get_content()).' " class="attachment-portfolio-medium size-portfolio-medium wp-post-image" alt="girl_bike-460x290"  sizes="(max-width: 460px) 100vw, 460px">';
                            $return_val .= '</div>';
                            $return_val .= '<h4>';
                                $return_val .= '<a href="'. esc_url($item -> get_permalink()).'">'.esc_html($item -> get_title()).'</a>';
                            $return_val .= '</h4>';
                            $return_val .= '<p>'. shorten($item -> get_description(), '150').'</p>';
                            $return_val .= '<p><a href="'. esc_url($item -> get_permalink()).'" class="more-link">Read more</a></p>';

                $return_val .= '</div>';
                     endforeach;
        return $return_val;
    }

add_shortcode( 'footag', 'rss_post' );

答案 1 :(得分:0)

我在我的主题的function.php中使用了这个函数:

function featuredtoRSS($content) {
        global $post;
        if ( has_post_thumbnail( $post->ID ) ){
            $content = '<div>' . get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'style' => 'margin-bottom: 15px;' ) ) . '</div>' . $content;
        } 
        return $content;
    }
    add_filter('the_excerpt_rss', 'featuredtoRSS');
    add_filter('the_content_feed', 'featuredtoRSS');