KISS Me, WordPress! Easy Category List for Stylesheet and ID for Pages

Posted on 28 March 2008 by LizzyFin

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Keep IT Simple, Stupid.

In my way of making things harder than necessary I stumbled upon something simple. At times nothing seems simple with CSS! Check it out.

Trying to style a particular post, based on whether it was a password-protected post or not, I found that using the category assigned could help - on the condition that one category name was reserved for the password-protected posts. Look at this piece of WordPress code:

<?php foreach((get_the_category()) as $cat)
{
echo $cat->category_nicename;
}
?>

Basically, what we are asking for here is to create an array that holds the names of the categories, and then return the nice names of each category in lowercase and with multiple words separated by hypens.

So, being able to assign category names on the fly gives us the ability to assign classes based on the category. Use the above PHP code snippet in the index.php, category.php and single.php files of your WP theme.

Remembering that we can assign more than one class to an xhtml feature, on line 5 or 6 in each of the 3 files replace

<div class="post"

with

<div id="post" class="post cat-<?php foreach((get_the_category()) as $cat)
{
echo $cat->category_nicename;
}
?>"

Now, in your stylesheet you can target classes like cat-owls, cat-hawks and cat-bald-eagles on your site about big birds.

The KISS part comes in where I was going through steps to create a page template to replace a Page that was already written so that I could style it differently than the rest of the blog. What? Instead of going through all that, all I needed to do was pick up the ID number of the “post” of that page. Duh!

How simple of me to forget that the pages, as well as the posts, that we write in WordPress are assigned ID numbers. Just use the #page-ID in your stylesheet! Oh yeah, pick up the ID numbers of posts and pages in the admin area under Manage/ Posts or Pages.

Too Many Plugins Slow Down Your WordPress Blog

Posted on 24 March 2008 by LizzyFin

I know you’re crazy about using plugins for your WordPress blogs. I am, too!

Plugins can enhance your blogs in so many ways. Do you want to encourage readers to sign up for your RSS feed? Is cutting down or eliminating comment spam important to you? How about optimizing your blog pings, streamlining your SEO efforts, automating backups, or even creating an archives page?

WordPress Plugins can be found for these and many other tasks. Check out the WordPress Plugin Directory where you can search over 1,500 plugins. Download as many as you want and try them out. No doubt you will find some real keepers.

The only caution here is that some plugins may not be updated to work with your WP version. Check the authors’ website to pick up the important details about the plugins you like. The best and most stable plugins are usually updated fairly quickly after WP is updated.

The downside to loading up your blogs with plugins is that too much of a good thing is…well, too much! Too many requests to your server to create your pages and your visitors are outta there. Why? It just takes too long. Also, all those plugins need to be kept up-to-date, so that means more time spent on blog maintenance.

Check to see if your theme and all the plugins you use are taking up too much server time by adding one line to the footer.php file of your theme:

<!-- <?php echo get_num_queries(); ?> queries -->

Essentially, we’re calling on the server to tell us how many times the server was accessed to create your page. The only way you will see the result of get_num_queries() is to go to your blog and View > Page Source. Look for a comment line in the footer, something like

<!-- 12 queries -->

If you see more than 20 or so queries, your server is being worked too hard and your pages will load slowly. Deactivate plugins, one at a time, and refresh your blog page. You’ll see the number of server requests, or queries, drop.

Delete plugins that are too costly in terms of the number of queries they demand and you’ll find a happy medium between too many requests that slow down your server and too few requests that make for a dull blogging experience.

How to Get Rid of that Uncategorized Category in WordPress Blogs

Posted on 22 March 2008 by LizzyFin

Until you’re very familiar with WordPress it can be difficult to find where to modify some of the features. It’s easy enough to add a new category for your posts right from the Write Post page. Over there on the right just type a new category in the box under Categories and click Add. Or add several categories at one time by separating them with commas.

The default post category can be changed from “uncategorized” to your choice of category by going to Options/Writing.

Writing options default post category of uncategorized.

Writing options shows the default post category of “uncategorized.”

At the default post category dropdown box, pick one you like and click on Update Options to make it so.

Add a new category by going to Manage/Categories. The trick here is that you can delete the uncategorized category after another category is selected as the default.

Manage categories for WordPress blogs.

Manage categories for WordPress blogs.

You picked the category you want as your default in the Options/Writing screen, or here you can click on add new to add more categories for your blog. Then, in the far right column, delete the uncategorized category.

A warning box pops up to alert you that you’re going to delete a category and that the posts that were assigned to the category to be deleted will now be assigned to the new default category.

Alert box shows you're about to delete a category.

Alert box shows you’re about to delete a category.

Click OK to make it so. You’ve gotten rid of the uncategorized category.