Teaching myself new WordPress tricks.
I made the big decision yesterday that I was going to combine the LangerBooks.com Web site, which is currently in a very sorry state these days, with this site and blog. But that opened up all kinds of new challenges for me.
The big challenge was making book-specific files and links available on just those pages that applied to the book in question. Although I could just put the links in a post and be done with it, I wanted a nicer solution. I wanted those links to appear in the sidebar for that book.
Keep in mind here that LangerBooks.com needs to support eight current titles as well as a catch-all title for out-of-print books. I created an individual WordPress category for each book and one for the catch-all. So I needed nine custom sidebars, as well as the sidebars that would appear on static pages, blog pages, date archive pages, etc.
There were a number of ways to do this and I chose what I thought was the most straightforward way. I used conditional statements to query WordPress about the type of page it was displaying and then generate a custom sidebar for that page on the fly.
This required me to roll up my sleeves and dig into PHP, which I don’t really know. Fortunately, I have a knack for looking at code and experimenting with it to see what it does. And although the search feature for WordPress.org’s Codex isn’t working right, Google can still find and display those documentation pages. So I did a lot of Google searching to find reference material, experimented, and came up with code that would work.
The unfortunate part was that there was no clear instruction to tell me what to do. There were some instructions that gave me bits and pieces of what I had to do. I just put things together to try them out and was very surprised when I didn’t get error messages.
The result is the custom sidebar that appears for the Book Support categories (I currently have 3 of the 9 done) as well as for single pages for a specific category. That’s where I got very fancy, using “and” and “or” codes to string PHP commands. Here’s what I mean; if you know PHP and WordPress, you’re likely to know what I did, too:
Put stuff to appear on every page here
<?php if (is_home()) { ?>
Put stuff to appear on the home page only here
<?php } elseif ( is_category('24') || is_single() && is_category('24') ) { ?>
Put stuff for MAC OS VISUAL QUICKSTART GUIDE here
<?php } else { ?>
Put stuff for pages that don't meet above criteria here
<?php } ?>
Put stuff to appear on every page here
(Note that to get that code to show at all, I had to put a space between the < and the ?. If you wanted to copy and paste this code, be sure to remove that additional space character.)
You can see the custom sidebar for yourself by visiting any of the categories listed under Book Support.
Of course, this is a highly simplified illustration of the code that works. In my example, I have lots of elseifs and more than one batch of conditional queries. This might be child's play for some of you WordPress power users, but it's all new to me and very rewarding when I get it right.
The trick when doing all this is to get the items you want to appear in the sidebar in the order you want them to appear on the pages they should appear. It's a good exercise in logic and I think it really helped clear the cobwebs out of my mind.
Three down, six to go. Time to finish up.
Later…
I’m about 90% done — done enough to go live with the “sites.” I still have to find and make available a bunch of files for books, including my Excel VQS and my two out-of-print FileMaker Pro books, which still seem to have a bit of life left in them. And I’d like to create a post for each book category that lists all editions of the book — some of them have quite a few editions. And maybe dig up and display all the book covers.
But for now, I’m done. My sidebar.php file is currently 450 lines long — and I don’t use many blank lines. I think I might make my entire theme, a highly modified and plugin dependent version of Exquisite, available for download when I’m finished with the WordPress book Miraz and I are writing.













1 response so far ↓
1 Tyler // Jan 30, 2008 at 2:58 pm
Hi!
I have sold an ad on my site that requires it be shown in the sidebar of the home page only. I am using wordpress and I am wondering if you could help me out.
Thank you,
Tyler
Tyler’s last blog post: Hurricanes and Rangers Mix it Up
Leave a Comment