WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom field on a category where you can add an image. I have it as a background image to show on the category's archive page, like so:


<body style="background:url('<?php echo z_taxonomy_image_url($cat->term_id); ?>')">

It uses the Categories Images plugin to achieve this.

Is there a way for the posts in this parent category to inherit this background image? something like $cat->parent_id?

share|improve this question
So example.com/category/parent and example.com/category/parent/child should have the same background? – developdaly Dec 3 '12 at 13:50
Exactly yeah, I would like the /child to inherit or reuse the image which is associated with the /parent – dodgerogers Dec 3 '12 at 13:56
up vote 1 down vote accepted

You can use this function to output the parent category ID and then style off of that.

function wpse_74737_category_top_parent_id($catid) {
    while ($catid) {
        $cat = get_category($catid);
        $catid = $cat -> category_parent;
        $catParent = $cat -> cat_ID;
    return $catParent;

And then call this in your body class: <?php echo wpse_74737_category_top_parent_id( $cat->term_id ); ?>

share|improve this answer
Hi developdaly this seems great! I don't think im calling it correctly in the single.php file though: <body id="<?php echo z_taxonomy_image_url($catParent); ?>"> – dodgerogers Dec 3 '12 at 14:03
Use <?php echo wpse_74737_category_top_parent_id(); ?> in single.php. – developdaly Dec 3 '12 at 14:04
Hmm can't get this to work, I have tried to print out the output of this fucntion but it comes up with nothing. What should this be outputting? something like domain.com/wp-content/uploads/2012/12/Virus_IQ1.jpg? – dodgerogers Dec 3 '12 at 14:24
No, you should be getting the ID of the parent category. I forgot to mention that you need to provide the ID of the current category. So something like, <?php echo wpse_74737_category_top_parent_id( $cat->term_id ); ?>. – developdaly Dec 3 '12 at 14:59
hey Developdaly I still need to fiddle with it but if you post the comment above as an answer ill gladly tick it. – dodgerogers Dec 3 '12 at 15:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.