• I am trying to show the RSS feed from one of my sites on another one of my sites. Thanks to the great folks here, I was able to get it to work, and things are almost exactly how I want it. The one missing piece is that I want it to show the number of comments for the posts displayed through the RSS feed.

    Here’s the code I’ve got so far, but I’m not getting the comment number to show. I’ve been trying various combinations using get_comment_number but nothing is working correctly.

    Ideally, I’d like for it to say something like “0 comments” then “1 comment” then 2 comments” depending (so plural for 0 and 2+ and singular for 1).

    <?php 
         $rss = fetch_feed('https://www.example.com/feed');
       if (!is_wp_error( $rss ) ) : 
         $maxitems = $rss->get_item_quantity(10); 
         $rss_items = $rss->get_items(0, $maxitems); 
       endif;
    ?>
    
    <?php function get_first_image_url($html) {
       if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
       return $matches[1];
         } }
    ?>  
    
    <?php
       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;
         }
    ?>
    
    <?php 
       if ($maxitems == 0) echo '<li>No items.</li>';
       else 
       foreach ( $rss_items as $item ) : ?>
    
                <div class="blog_image" >
                <figure>
                <?php echo '<img src="' .get_first_image_url($item->get_content()). '"/>'; ?>
                </figure>
                </div>
    
                <div class="blog_title"><a href='<?php echo esc_url( $item->get_permalink() ); ?>' title='<?php echo esc_html( $item->get_title() ); ?>'> <h3><?php echo esc_html( $item->get_title() ); ?></h3></a>
                </div>
    
                <div class="blog_date_main" >&nbsp;Posted by: <?php echo get_the_author_meta( 'display_name', $post->post_author ); ?>
                | Posted on: <?php echo $item->get_date('j M Y'); ?>
                </div>
    
                <p class="blogs-excerpt-content"><?php echo shorten($item-> get_description(),'432');?></p>
    
                <div class="blog_read_more"><a href='<?php echo esc_url( $item->get_permalink() ); ?>' title='<?php echo esc_html( $item->get_title() ); ?>'><?php esc_html_e('Read More','new_style'); ?></a>
                </div>
                <div class="main_like_comment">
                <?php
                /** here is what I want it to say XX Comments */ ?></span>Comment
                </div>
    
    <?php endforeach; ?>

    Any help would be greatly appreciated.

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘comment number in RSS’ is closed to new replies.