Custom Loop in Sidebar

Filed Under: Blogging, WordPress

If you’re a rabid WordPress fan like me you no doubt love to peruse the documentation at the Codex. I know I do. I could spend hours pouring over the information and quite often I do. Usually after coming across some useful little nugget I’ll hurry over to my local install and put theory to practice. I think half the fun is reading about things like conditional tags then trying to conjure up some new and unique way to use them. Some times I’ll break something because I just can’t get it to do what I want at first but who cares? It’s localhost. Experiment away!

Anyway, this post is not about the use of conditional tags. Have you ever come across those blogs that have a feature in the sidebar along the lines of Popular Posts or Must Reads or some such thing? Have you ever wondered how they do it? Would you like to learn how to do it? While some of these implementations probably make use of plugins I’ll show you a way to do it without them by using a custom Loop combined with Query_posts. In sidebar.php place the following …

<h3>Popular Posts</h3>
<ul>
<?php query_posts('category_name=wordpress'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><br />
<?php the_excerpt(); ?></li>
<?php endwhile; ?><?php endif; ?>
</ul>

What is this? The query_posts function in the line before the Loop calls for a specific Category of your choosing. The next line says if we have a post, while we have a post, execute the post. The line after that is simply the permalink of the individual post along with its title while immediately after that is the_excerpt() - you can use the_content() instead but you’d probably run out of real estate fairly quick. Remember, this is going in your sidebar. The last bit merely closes the while and if statement.

Now there’s a ton of different ways you could customize this. For instance, you don’t necessarily have to call the Category by name, you could use it’s numerical ID. Instead of using category_name=NAME you could use cat=ID where ID is the actual ID number of the Category. The query_posts function would look like this …

<?php query_posts('cat=123'); ?>

What if you want to display a certain number of posts? By default, this will display the number of posts as per your settings in the admin interface. In other words, if you have your main page set to display 5 posts then this will also display 5 posts. But that’s easy enough to get around by making use of the showposts= parameter. Assuming your main page is set to display 5 posts but you want to show 7 posts in your sidebar then you pass the parameter to the query_posts function as such …

<?php query_posts('category_name=wordpress&showposts=7'); ?>

You can pass multiple parameters but remember to precede each additional one with &. Below is a screen capture (click to enlarge) of this in action in my own blog. I’ve since removed it as the only reason for using it is to show you what it looks like.

Custom Loop in Sidebar

Further reading:
http://codex.wordpress.org/The_Loop
http://codex.wordpress.org/Template_Tags/query_posts

 

Thanks for visiting! If this is your first time here why not grab the RSS feed. You can also follow this discussion by checking the "Notify me of followup comments via e-mail" box or by subscribing to the comments RSS feed. Please scroll down to leave your comment.

11 Comments so far. Add Yours Now!

  1. Particularly, when it comes to coding, I’m just curious about it. I can’t wait to get my hands on Wordpress :)

  2. You’ll love it. There’s so much that can be done with it. In fact, through the use of advanced features such as “custom fields” WordPress can be used as a full blown CMS and not just a blogging tool.

  3. Ismail,

    I should mention that you can play around with WordPress before you even get a host. Download and install a program called XAMPP. This app will install Apache webserver and MySQL database on your computer (which WordPress needs to run) with complete access via phpMyAdmin. Once you have it up and running download and install WordPress. I recommend this to all WordPress users - newcomers and current users alike.

    I use XAMPP for 2 reasons:
    1. To maintain a complete offline backup of my real blog
    2. For experimenting and troubleshooting

    You can get XAMPP for Windows from here -
    http://www.apachefriends.org/en/xampp-windows.html

    I chose the Basic Package - Installer [34MB]

    Easy instructions on how to install XAMPP from here -
    http://www.tamba2.org.uk/wordpress/xampp/

    The above instructions are for XAMPP Lite but are easily adaptable for other XAMPP packages such as the one I have installed.

    And of course you get WordPress from here -
    http://wordpress.org/download/

  4. Thanks for sharing. I will try it out later but as I said earlier, anything got to do with the Wordpress, for sure I’ll keep in touch with you :)

  5. You were saying that if I’m using the free Wordpress, I can’t tweak and hack the CSS and the Html except continue using with what they offered?
    For example, the only theme allowed is the Kubica Theme and I can’t access to their CSS only if I upgrade my account. Am I rite?

    Now you’re telling me that I could still do the tweaking and hacking of the template as long I did install the above application.

  6. Yes. If you use the free hosting provided by WordPress.com you are limited as to what you can do with it. Even if you purchase their upgrade that only allows you access to the CSS. You still cannot upload your own themes. You cannot upload plugins. Wordpress.com is great for people who want to see what the software is all about, to get a taste. But to realize the full power of the software you have to download and host it yourself.

    The problem is WordPress needs a webserver (preferably Apache) and a database (preferably mySQL) to run. XAMPP will install these services on your computer so that you can run WordPress right on your own PC.

  7. Thanks, very informative, again what’s the minimum requirements to install the above app on my pc?

  8. All you need is WindowsXP Home. If you’re running XP Pro you’ll have to stop the IIS service. WordPress and IIS don’t play nice.

    The minimum requirements for WordPress are:
    1. PHP version 4.2 or greater
    2. MySQL version 4.0 or greater
    3. Apache or LiteSpeed webserver (preferably Apache)

    XAMPP for Windows will install these services. As I said earlier, the tutorial on installing XAMPP was written for XAMPP Lite but I easily adapted it for XAMPP Basic Package Installer 34MB.

  9. The above app is almost like using blogger. No ftp required :)

  10. Correct me if I’m wrong. By using the above app, means I could have my own htaccess Password and htaccess Banning as well into my Wordpress. Am I right?

  11. Absolutely. If you choose to change the default WP permalink structure to use “pretty permalinks” WordPress will automatically create the htaccess file because of course WP uses that file in conjunction with Apache’s mod_rewrite module to generate the pretty permalinks. Otherwise you can manually create it. And of course you’re familiar with the power of a htaccess file. ;)

    The other neat thing about XAMPP is you could actually host your WP blog right from your computer until you find a host if you wanted.

Reply to “Custom Loop in Sidebar”

Please note: Comment moderation is in effect. It may take some time for your comment to appear. There is no need to resubmit it.

Management does not necessarily endorse or agree with comments left here. Management reserves the right to not publish comments it deems unfit. Play nice.

 

Subscribe

Click below to subscribe.

  • Subscribe to the feed!

Catch it Live!

Live weekly show featuring the movers and shakers of WordPress.

  • WordPress

Just Say No!

To themes using obfuscated code.

  • Say No to Obfuscated Code!

Powered By ...

Is there anything else? Anil?

  • Powered by WordPress!