Monday, September 14th, 2009
Recently, one site we were working on needed a sidebar that would show the 4 most recent posts on the site. But here’s the catch: they wanted only one post per author. And, each author had to have the role of “author” as opposed to contributor or admin.
The site in question is still in Beta, so I can’t give out the link yet. But here’s a snapshot to give you a better idea of what we were going for.

We also used the plugin User Photo to show each author’s thumbnail, which I have come to love and recommend.
Thanks to Mark Kaplun for this great solution:
Open your Sidebar.php file and add the following:
Please note: this code uses the plugin Limit Posts to create excerpts. But you can substitute that snippet with any type of excerpt code.
<?php $authors = get_users_of_blog(); ?>
<?php
$latest_posts = array();
foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );
if ($user->has_cap(‘level_7′))
continue;
$ps = get_posts(‘numberposts=1&post_type=post&author=’ . $author->ID . ‘&cat=-9,-3′);
foreach ($ps as $p) {
$latest_posts[$p->post_date] = $p;
}
}
krsort($latest_posts);
?>
<?php //query_posts(‘author=’ . $author->ID . ‘&showposts=1&cat=-9,-3′); ?>
<?php
$counter =0;
foreach ($latest_posts as $post) {
$counter++;
if ($counter > 4)
break;
setup_postdata($post);
?>
<?php // while (have_posts()) : the_post(); ?>
<li>
<?php userphoto_the_author_thumbnail(); ?>
<span class=”blogauthor”> <?php the_author_posts_link(); ?> </span>
<div class=”blog”> <a title=”Permanent Link to <?php the_title(); ?>” href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></div>
<br />
<?php the_content_limit(‘55,read more’); ?> <strong><a title=”Permanent Link to <?php the_title(); ?>” href=”<?php the_permalink() ?>”>Read more</a> </strong>
</li>
<?php } ?>
<?php // }; ?>
The End. Yay code!
Posted in Code Snippets | Tags: author, list, recent posts, sidebar, thumbnails, wordpress | 4 Comments »
Thursday, November 20th, 2008
You can manage multiple sidebar widgets in WordPress. To do so, you go to the Widgets page in the Admin, and select the Sidebar you want to manage. If you have 1 widgetized sidebar, the name “Sidebar 1″ is not a big deal for managing it. But what if you have 5 or more, and they’re named Sidebar 1, Sidebar 2, etc. Ah yes, now what was that wily sidebar 3 for?
Why would someone have so many sidebars to begin with? Well, remember that you can also add widget-ready sidebars to Wordpress footers or anywhere in your design, to give extra content management options to clients.
Recently, we had a client with way too many widgetized sidebars to keep track of, so we had to find a new solution to change the standard widget sidebar names like “sidebar 1″ or “sidebar 2″ to something more meaningful like “Left sidebar” and “Right sidebar” in the admin area. I dug around the web and found 2 very helpful posts:
Here’s what I learned:
1. Go into your themes’s function.php file, or if it doesn’t exist, create it.
Add the following code:
<?php
if (function_exists(‘register_sidebar’))
{
register_sidebar(array(
‘before_widget’ => ‘<li>’,
‘after_widget’ => ‘</li>’,
‘before_title’ => ‘<h4>’,
‘after_title’ => ‘</h4>’,
‘name’ => ‘Left sidebar’
));
register_sidebar(array(
‘before_widget’ => ‘<li>’,
‘after_widget’ => ‘</li>’,
‘before_title’ => ‘<h4>’,
‘after_title’ => ‘</h4>’,
‘name’ => ‘Right sidebar’
));
}
?>
2a. Add a widgetized sidebar to your theme. Open up your theme’s sidebar file (for exampe, l_sidebar.php) and look for the first <ul> or <ul id=”sidebar”> or something similar, and add the following code:
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘Left sidebar’) ) : ?>
2b. If your sidebar is already widgetized, find the following code
<?php if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(1) ) : else : ?>
and replace it with
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘Left sidebar’) ) : ?>
Then, find the closing </ul> at the very bottom of the file, and immediately before that, place
<?php endif; ?>
3. Now, if you go into your admin panel, under Design>Widgets, you’ll see the new names like in the image below.

Now you can easily manage your Widgets without trying to guess which one is which.
Posted in Code Snippets, WordPress as CMS | Tags: functions, names, sidebar, widgetized sidebar, widgets | 1 Comment »
Friday, February 8th, 2008
One of the reasons I hesitated to use widgets is because there are many useful plugins that aren’t widgets and demand that you add a snippet of code to your sidebar, something you can’t really do with widgets.
Well, now you can widgetize your sidebar and add both widgets and non-widget plugins to it with the Custom Functions Widgets plugin.
When activated, this plugin creates 20 empty widgets. To use one of them for a non-widget plugin, you drag one onto the sidebar, and then edit the widget’s options as follows:
- Give it a title.
- Name the function you want to call.
- Enter the arguments to pass.
- HTML to display before the output of the function
- HTML to display after the output of the function
- Option to hide the title of the widget (which you specified as Widget title) during the output
- Option to remove the default wrapper of the widget (as defined in
template.php of your theme’s folder, for each sidebar)
- Close the widget options pop-up and click ‘Save Changes’
Now your sidebar can handle all types of plugins and widgets!
WordPress Plugin: Custom Function Widgets
Posted in Plugins | Tags: sidebar, widgets | 1 Comment »
Thursday, January 24th, 2008
Headsetoptions has a great review of the types of tabbed interfaces available, and a tutorial on how to add one to your WordPress blog.
A tabbed interface is one of those boxes that you often see in the sidebar of a lot of premium themes where you click on tabs at the top, and it shows you different types of information in the same space. The main benefit of tabbed interfaces is that it helps save real estate on your blog, which can be pretty limited. Here’s an example from Solostream’s Premium WordPress Magazine Theme:

Solostream also has a tabbed interface for the feature article section in some of his other premium themes.
Basically, there are three tabbed interfaces available: DOMtab, Tabber, and Tabs. The main difference between the tabbed interfaces are ease of use, and whether you can use them for commercial purposes.
Update Jan. 26, 2007: Another tabbing method that wasn’t mentioned on Headsetoptions is Yahoo! UI Library: TabView.
Check out the full post for the whole shebang: Anatomy of a Magazine Style Premium WordPress Theme – Part 2: DOMTab, Tabber, more
Posted in Tips | Tags: navigation, sidebar | 2 Comments »
Saturday, March 31st, 2007
Some of you may have noticed that I added a Finetune player in the right-most sidebar. Why? Only because I LOVE Finetune. As opposed to the other online music services, it actually lets you choose the songs you want to hear, as opposed to guessing what songs you like based on songs or artists you choose. I put together a playlist of my favorite songs on Finetune, and I love listening to it. You know how fun it is when your favorite song comes on the radio? Well, with Finetune you only hear your favorite songs.
I put the player on this blog for fun. It may not be there for long, since it really has nothing to do with this blog except the fact that the player can be embedded on it. But if you love music, and you have very specific taste, take a look at Finetune and put together a playlist – it’s a lot of fun.
Posted in Tips | Tags: multimedia, sidebar | No Comments »
Sunday, March 25th, 2007
In an earlier post, I wrote about my quest to find a way to easily allow users to modify text in the sidebar of a site I was creating. After trying a few different options, I happily settled on the Improved Include Page plugin.
Then I started trying to add all sorts of social stuff to the blog part of the site, such as the Sociable icons for bookmarking, and the option to email a post to a friend, or print a printer-friendly version. The problem is that if you select to have the sociable bookmarks appear on Pages, and you insert the PHP code for the print and email functions on Pages, it conflicts with the bit in the sidebar which is also, in essence, a Page.
After playing around with it for a bit, I realized I needed a different solution to the editable text in the sidebar, and decided to try to go with widgets.
The Need: I needed to be able to insert a text widget so that users could easily edit the text within it, but I needed to keep the rest of the information that appears in the sidebar, such as the posts from a certain category, since as far as I know there are no widgets that allow you to display posts from one category only. I also needed the text in the widget to appear with the same styles as the rest of the sidebar.
The Solution:
First I made the site widget-ready. This is very easy, and involves only a few steps:
- Download the widgets plugin. Upload the entire widgets folder to your plugins directory. Activate.
- Create a file called functions.php. Put the following code in the file and upload (make sure there are no spaces before or after the code – that can be deadly!):
<?php
if ( function_exists('register_sidebar') )
register_sidebar();
?>
- Since I wanted most of the sidebar text to remain as is, I modified my sidebar as follows:
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="s" id="s" value="search and press enter..."/></form>
<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar() ) : else : ?>
<?php endif; ?><h2>News</h2>
<ul>
...
This kept my search bar at the top, inserted a place for the widgets, and then after that the rest of the sidebar appears as is.
- The widgets plugin that was installed in step 1 creates a menu under Presentation>Sidebar Widgets. I went there, and dragged a text widget onto the Sidebar. I then clicked on the box on the right, and entered the title in the title field, and the text in the text box with HTML tags.
- Everything looked good, but next to the title was an annoying bullet that obviously emanated from an unwanted <li> list tag that had been added. The question was – where did it come from? I scoured the web looking for an answer, and found a hint on this page under the section called “My sidebar isn’t a list. What do I do?” It explained that it styles the widget with <li> tags. I opened widgets.php in the widgets folder under plugins, and changed lines 52 and 53 as follows:
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => "</div>\n",
I changed the places where it said li to div. It worked like a charm.
Now I had easily editable text in the sidebar, plus I could freely use the Sociable, e-mail and print plugins as I pleased.
Posted in Plugins | Tags: Pages, sidebar, widgets | 5 Comments »
Tuesday, March 20th, 2007
Update: since I wrote this post, I discovered some problems with this solution. I ended up using a different solution, which you can see in Inserting editable text in the sidebar – part II: Widgets.
I created a site for a client based on WordPress that needed some dynamic editable text in the sidebar aside from the usual blog-type stuff like archives and categories. They had a section they needed to be able to update frequently and easily from the backend.
First I tried to use Kaf’s Get-a-Post plugin. I put the required code in the sidebar, and the owners of the site could edit the specified post and the updated content would appear on the site. But the plugin did weird stuff to the text that appeared under that part on the sidebar. It was creating links where there shouldn’t be links, and was changing the formatting of the h2’s.
Next I tried Kaf’s Welcome Visitor plugin. This creates a new link under Options in the backend, and on that page you can enter text in what is very similar to the regular WordPress Write Post or Write Pages format. On this Welcome page you can enter a title and text, and after putting the correct code on the sidebar (or wherever you want it to appear), that text and changes you make to it appear there. But this plugin had even more quirks: the Welcome link under Options only appeared to users with administrative privileges, and not to anyone else; and it disappeared on the front end to any users that are logged in to the site. So I ruled this one out too.
And then I found the answer: Improved Include Page plugin. I created a page with the name that should appear as the header on the sidebar. I made sure to code the header so that this page wouldn’t appear on the navigation bar by entering the following code where the navigation bar should appear:
<?php wp_list_pages('exclude=12&sort_column=menu_order&title_li='); ?>
12 is the page ID, and the “exclude” code tells the page not to display that particular page name in the navigation bar.
Then I put the following code in the sidebar where I wanted the text on that page to appear:
<?php if(function_exists('iinclude_page')) iinclude_page(12,'displayTitle=true&titleBefore=<h2>'); ?>
Now this code tells the page to display the Page with ID of 12. Title=true means it will display the title of the page, and titleBefore=<h2> means that the title will be styled with h2 tags.
And it worked! You can see the results here: www.kayema.com. Look at the top text on the sidebar, where it says “KSI Quote” – that header plus the numbers and date underneath are editable in the backend on the page called “KSI quote.”
Improved Include Page>>
Posted in Plugins, WordPress as CMS | Tags: Pages, sidebar | 6 Comments »
Wednesday, February 21st, 2007
To show a different sidebar on different pages, replace the standard call for the sidebar:
<?php get_sidebar(); ?>
with the following code:
<?php include ('sidebar2.php'); ?>
The same can be done to call different headers, i.e. instead of <?php get_header(); ?> use <?php include ('header2.php'); ?>For more information, see Different Sidebars Anyone? on the WordPress codex.
Posted in Code Snippets, Tips | Tags: sidebar, template tags | 4 Comments »