How to Create a Slick Javascript Sliding RSS Reader in WordPress

Posted in these categories: Tutorial, WordPress

I’ve been learning lots of new WordPress tricks this past week.  Writing them down is two-fold win for me because 1) you guys get a look into how I do some of the stuff I do, and 2) I don’t forget how to do these tutorials later on when I need them again.  Yes, that can be a problem sometimes for me.  Don’t judge me.

Anyway, Scott Raynovich over at the Rayno Report wanted a fantastic way to showcase RSS feeds from around the web.  He wanted something that would allow more than four or five feeds without taking up the whole sidebar.

The result?  This really slick Accordion based feed sidebar:

image

Clicking on one of the titles…

image

Everything slides down to make room. 

It was actually really simple to create, and the best part: it’s widgetized!  That’s right, any feed that needs to be added in is simply added in as an RSS widget to a special sidebar.

So, onto the how-to!

Our process includes the following:

  1. Set up and Install the Accordion Script (and Google-Load jQuery)
  2. Set up a second sidebar (or third, or whatever number you have)
  3. Add the appropriate code to the sidebar.php file
  4. Add in a few widgets to test

Step 1: The Accordion Script

I know that there’s a jQuery-UI accordion script that’s floating around, but we’re going to be using a bit of a modified version that’s set up specifically for WordPress.  I like to pre-load via Google jQuery as well – takes some of the load off of the server and allows it to do its thing, letting Google fit the processing power for the actual framework.

Insert this code into your header:

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js”></script>
<script type=”text/javascript”>
function initMenus() {
    $(‘ul#accordion li ul’).hide();
    $.each($(‘ul#accordion’), function(){
        $(‘#’ + this.id + ‘.expandfirst ul:first’).show();
    });
    $(‘ul#accordion li h2.widgettitle’).click(
        function() {
            var checkElement = $(this).next();
            var parent = this.parentNode.parentNode.id;

            if($(‘#’ + parent).hasClass(‘noaccordion’)) {
                $(this).next().slideToggle(‘normal’);
                return false;
            }
            if((checkElement.is(‘ul’)) && (checkElement.is(‘:visible’))) {
                if($(‘#’ + parent).hasClass(‘collapsible’)) {
                    $(‘#’ + parent + ‘ ul:visible’).slideUp(‘normal’);
                }
                return false;
            }
            if((checkElement.is(‘ul’)) && (!checkElement.is(‘:visible’))) {
                $(‘#’ + parent + ‘ ul:visible’).slideUp(‘normal’);
                checkElement.slideDown(‘normal’);
                return false;
            }
        }
    );
}
$(document).ready(function() {initMenus();});
</script>

The first script tag loads jQuery, the second the accordion script.  Notice how we’re targeting ul#accordion as the feed sidebar, and h2.widgettitle as the trigger.

Step 2: Adding a Second Sidebar into the Functions

Head into the theme editor and navigate to your theme functions file (functions.php).  We’re going to 1) make sure there’s widgetized sidebar code inside and 2) make sure that it calls for more than one sidebar.

If you have:

<?php
if ( function_exists(‘register_sidebars) )
    register_sidebar(array(
        ‘before_widget’ => ‘<li id=”%1$s” class=”widget %2$s”>’,
        ‘after_widget’ => ‘</li>’,
        ‘before_title’ => ‘<h2 class=”widgettitle”>’,
        ‘after_title’ => ‘</h2>’,
    ));

?>

or something similar, you’re already widgetized.  Change that code to what comes next to get it multiple-sidebar ready.  If you aren’t widgetized, add in this bit of code at your own risk:

<?php
if ( function_exists(‘register_sidebars’) )
    register_sidebars(2,array(
        ‘before_widget’ => ‘<li id=”%1$s” class=”widget %2$s”>’,
        ‘after_widget’ => ‘</li>’,
        ‘before_title’ => ‘<h2 class=”widgettitle”>’,
        ‘after_title’ => ‘</h2>’,
    ));

?>

This sets up two sidebars.  The second sidebar will be where our feeds go.

Step 3: Adding a Second Sidebar

Save your file and switch to sidebar.php.  If you’re widgetized you should see something like this:

  <ul id=”sidebar”>
            <?php     /* Widgetized sidebar, if you have the plugin installed. */
                    if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar() ) : ?>
            <?php endif; ?>

   <!—OTHER SIDEBAR STUFF—>

   </ul>

Somewhere in that file (either before the if statement, or after), you want to add in this:

<li class=”feedreader”>
  <h2 class=”widgettitle”>RSS Feeds</h2>
  <ul id=”accordion” class=”collapsible”>
                <?php     /* Widgetized sidebar, if you have the plugin installed. */
                    if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(2) ) : ?>
            <?php endif; ?>
</ul></li>

This adds in a widget that will serve as the container for the accordion file.  Then, we have another unordered list that will contain the feed widgets we drop in.

You should have something that looks like this:

  <ul id=”sidebar”>
            <?php     /* Widgetized sidebar, if you have the plugin installed. */
                    if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar() ) : ?>
            <?php endif; ?>

<li class=”feedreader”>
  <h2 class=”widgettitle”>RSS Feeds</h2>
  <ul id=”accordion” class=”collapsible”>
                <?php     /* Widgetized sidebar, if you have the plugin installed. */
                    if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(2) ) : ?>
            <?php endif; ?>
</ul></li>

   <!—OTHER SIDEBAR STUFF—>

   </ul>

That will insert your new sidebar. 

Step 4: Adding Widgets

After that, the last thing to do is to add in some feeds to test out in the Widgets panel of the Appearance tab.  Add in two or three to get a test, then view the page.

Any styles can be applied to make things look better, and here’s what you can expect to be able to change:

ul#accordion – controls the entire accordion box

ul#accordion h2.widgettitle – controls what the title feed of the individual feeds looks like

ul#accordion li li – individual feed items

ul#accordion li a – individual feed link properties

Head over to the Rayno Report to see it in action (and read his content – LOTS of great tech tips from the financial side).

  • Len Fischer
    Hey Mitch. This is an impressive effort. I have built accordion treatments like this in sidebar rails adjacent to news articles as a way to present video, graphics, infoboxes and related links/sites in a space-efficient manner that doesn't overwhelm readers. It's also useful in helping to visually balance news article pages where the sidebar, which might also include advertising and promos to other content, can end up being longer than the article itself. My accordions also are widgetized in a similar fashion, inserting dynamic content using our content management system's scripting language, just like WordPress' RSS widget inserts the feed headlines. IMHO, the real challenge is to create an accordion-style component that can work on any CMS platform without proprietary plug-ins or scripting. Such a component would do it all: fetching the RSS, parsing it and presenting it within the accordion using nothing more than HTML, CSS and JavaScript. I know something like this can be created using a combination of the Google Feed, Search and AJAX APIs, but I don't know if I'm ever going to find the time or have the patience to build it. LOL. Maybe you will.

    Pipedreams aside, I recommend that you recheck your WordPress solution's code because it doesn't appear to render correctly in WebKit-powered browsers such as Chrome (4.1.249.1036) or Safari (4.0.4), even though it looks great in the latest version of Firefox running under Windows Vista (didn't check IE because I couldn't bring myself to power it up, but I'd look there, too). It's difficult to tell what's going wrong, and it might have something to do with the WordPress plug-in that fetches the RSS, but I'd bet it's something that just requires a few simple tweaks or workarounds so it works correctly in all Grade A browsers.

    Aside from browser compatibility, you also might consider adding some visual cues to the accordion presentation to help readers better understand what it is and how it works. As designers and developers, we intuitively "get" how an accordion works, but if I sat my mother, for instance, in front of it and asked her what the tabs did, she probably would tell me they are just links to other sites, and if she clicked them she'd leave the site and go to whichever one she clicked on. That's not surprising because the tabs themselves are so similar to the site's top navigation. My mother may be an extreme case, but even more experienced users might not immediately recognize the tabs as an accordion because they look just like the site navigation. The simplest solution might be to include a line of text under the Around the Web heading that reads: "Click the publication names to see their latest headlines via RSS" or something to that effect. What's more, I sometimes use a graphical plus-sign (+) treatment in accordions to help readers understand that when they click a link, it will expand to show more information. I also have used an arrow that points to the right (directly at the clickable link) when a tab is closed. When the link is clicked, the arrow toggles to point down at the new links revealed beneath the active tab. These visual cues aren't difficult to add and help to guide readers. I believe they also could add a touch of subtle sophistication to the accordion, without overwhelming its simple presentation. Just food for thought and maybe something to consider.

    Once again, I think the accordion solution for displaying RSS links or any collection of categorized links works very nicely, and the way you are handling it via WordPress is clever and relatively easy to implement. I am sure that you'll see at least a few site owners interested in adding the same technique to their sites very soon - and once your solution is tweaked for all browsers.

    Cheers and good luck!
  • Len,

    Thanks so much for the long and detailed comment. We had moved some stuff around on Scott's site and (since I was only moving items and not adding anything new) I didn't bother checking the site in the webkit browsers.

    It turns out that jQuery cycle didn't like the fact there was only one entry (as it's post/category driven) and decided to pick up ALL of the widgets as entries, and not just the cycle <div> tags itself. A quick addition of a secondary sidebar to contain the cycle and subscription widget, and things are back up and running. It was a rare case because not everyone will have a jQuery cycle widget running above this sidebar, and once the problem was debugged it was a fairly easy fix.

    This code DOES indeed work in webkit browsers, however, and you can retest the site in both Safari and Chrome as I did to see that everything is running smooth and creamy.

    Thanks for pointing this out to my attention, though, as well as the UI suggestions. I have a few last minute tweaks to add to the site, and one of them (as I had put on the comp but have not yet implemented) is an acronym tag or anchor tag with the title text explaining how it works.

    Once again, thanks for taking the time to comment - even if it's pointing out a mistake that means that you're as passionate about it as I am, and I'm always looking for more people of that mindset to hook up with.</div>
blog comments powered by Disqus