• dustinw

    (@dustinw)


    Hi all,

    I was hoping someone could lend me a hand. I’m building a theme, and using the fetch feed (Link Here) to pull in an RSS feed. My problem though, is that I’d like to style each alternate post differently. For instance the first post being a gray background, and the second white. I’m not entirely sure how to go about this…

    Also, does this method pull in images? I didn’t see any mention of it, and it doesn’t seem to pull them in…

    Any help would be greatly appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • jrav001

    (@jrav001)

    Can’t be very specific without seeing your code, but a simple counter on your display loop will allow you to style based on odd or even numbers. As for the images, print_r the array to see where the images are located.

    Thread Starter dustinw

    (@dustinw)

    Sorry for not posting the code:

    <?php include_once(ABSPATH . WPINC . '/feed.php');
    
    $rss = fetch_feed('https://myrssfeed');
    if (!is_wp_error( $rss ) ) : $maxitems = $rss->get_item_quantity(5);
        $rss_items = $rss->get_items(0, $maxitems);
    endif;
    ?>
    
    <ul>
        <?php if ($maxitems == 0) echo '<li>No items.</li>';
        else
        // Loop through each feed item and display each item as a hyperlink.
        foreach ( $rss_items as $item ) : ?>
        <li>
            <a href='<?php echo $item->get_permalink(); ?>'
            title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
            <?php echo $item->get_title(); ?></a>
        </li>
        <?php endforeach; ?>
    </ul>

    It’s pretty much straight from the example.

    Note: I removed the rss name from the code. The feed displays just fine, just want to style it more. ??

    jrav001

    (@jrav001)

    This is untested….

    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    
        else
    
        $color1 = "GreyBG";  // class for grey background
    	$color2 = "WhiteBG"; // class for white background
    	$row_count = 0;  // counter
    
        // Loop through each feed item and display each item as a hyperlink.
        foreach ( $rss_items as $item ) : 
    
        $row_color = ($row_count % 2) ? $color1 : $color2; // test for odd or even
        echo "<div class==\"$row_color\">\n"; // apply the class
        ?>
    
        <li>
            <a href='<?php echo $item->get_permalink(); ?>'
            title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
            <?php echo $item->get_title(); ?></a>
        </li>
        <?php
        </div> // end row_color
        $row_count++;
        endforeach; ?>

    To find out what information is contained in the feed, after this line:
    $rss_items = $rss->get_items(0, $maxitems);
    Add this:
    print_r ($rss_items );

    Thread Starter dustinw

    (@dustinw)

    Thank you for the help jrav! I got it working based on your code.

    Thanks again!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Styling rss feed stream’ is closed to new replies.