Hi,
Been a while. Hope you're all doing good.
Thought I'd share this with you. The more super genius types may know
a better way to do this, but thought I'd share it as I've got a warm
glowy feeling from figuring it out.
So, you're in the middle of doing a website about books.
You've got a post about each book.
The title of the post is the title of the book.
Which is just fine and dandy.
As more than one author can have more than one book, you decide to
make each author a sub-category (child!) of a category (parent!)
called 'author', and you stick each book (post!) in its appropriate
'author' sub-category (child!).
For now, we'll ignore the problem of what happens if you ever get more
than one author with the same name. (It's called running around in
circles making 'squeeeeeeeee!' noises.)
So, you've entered all your book posts and everything is just super
wonderful.
You start making your theme.
Getting the title of the book is easy, as that's what the_title() is
for, right?
But what about the name of the author? I mean, ye gods, it's buried in
all that category rubbish right?
Well, pish and tish and old wet fish.
Try this.
First go here:
http://codex.wordpress.org/Template_Tags/in_category#Testing_if_a_post_is_in_a_descendant_category
That page talks about a function called post_is_in_descendant_category
that you add to your functions.php file in your theme folder.
M'kay?
Then in your single.php (or wherever you want to put your loop), do
something like this…
if (in_category( 'THE_NAME_OF_A_PARENT_CATEGORY') ||
f_post_is_in_descendant_category(THE_ID_MUMBER_OF_A_PARENT_CATEGORY) )
{
foreach((get_the_category()) as $category) {
if($category->category_parent==THE_ID_MUMBER_OF_A_PARENT_CATEGORY) {
$author=$category->cat_name;
}
}
}
Then you can do echo $author and bingo! Booker Awards for everyone!
Seems to work for me anyway.
Hope this helps someone.
Feel free to rip this bits if you think I'm being stupid.
Best,
Alan.
P.S. From October MDDA can open it's doors to user groups again one
Wednesday per month. A new member of staff is starting on my team
then, which means I can cover the rooms on that night.