Getting the name of a child category a post is in

3 views
Skip to first unread message

Alan Holding

unread,
Sep 3, 2009, 4:19:41 PM9/3/09
to Manchester WordPress User Group
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.

Alan Holding

unread,
Sep 3, 2009, 4:22:03 PM9/3/09
to Manchester WordPress User Group
Oops!

I meant…

if (in_category( 'THE_NAME_OF_A_PARENT_CATEGORY') ||
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;
}
}
}

Ho hum.

Alan Holding

unread,
Sep 5, 2009, 4:06:40 PM9/5/09
to Manchester WordPress User Group
Actually, after a think, I can replace the above with a function that
makes does an SQL query to the WordPress database.

Something like this…

In this example:
- $id is the id of a WordPress post - wp_posts.id
- 37 is the is id of the parent category ('author') of which the name
of the author is a sub-category / child -
wp_term_taxonomy.term_taxonomy_id

No error checking and not sure if having so many sub-queries in the
SQL is good or not, but seems to execute quite quickly.

function f_mbaGetBookAuthor($id) {
global $wpdb;
$mba_sql = "
SELECT name
FROM $wpdb->terms
WHERE term_id
IN (

SELECT term_taxonomy_id
FROM $wpdb->term_taxonomy
WHERE parent = 37
)
AND term_id
IN (

SELECT term_taxonomy_id
FROM $wpdb->term_relationships
WHERE object_id = $id
)
LIMIT 0 , 1
";
$mba_author = $wpdb->get_var($mba_sql);
// Should return a string
return $mba_author;
}

Might be of use to someone out there.
Reply all
Reply to author
Forward
0 new messages