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?

  • So example.com/category/parent and example.com/category/parent/child should have the same background? Commented Dec 3, 2012 at 13:50
  • Exactly yeah, I would like the /child to inherit or reuse the image which is associated with the /parent Commented Dec 3, 2012 at 13:56

1 Answer 1


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 ); ?>

  • 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); ?>"> Commented Dec 3, 2012 at 14:03
  • 1
    Use <?php echo wpse_74737_category_top_parent_id(); ?> in single.php. Commented Dec 3, 2012 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? Commented Dec 3, 2012 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 ); ?>. Commented Dec 3, 2012 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. Commented Dec 3, 2012 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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