WordPress: How do I show the parent category in the post meta?

I have a WP site here: http://www.undergroundsound.com.au

As you can see each post on the front page says “Posted in ‘child categories’”. I would like it to say something like “Posted in ‘parent category’ – ‘child categories’”.

Read More

This is the code that needs to be edited, in content.php:

<?php printf( __( 'Posted in %1$s', 'underground_sound' ), $categories_list ); ?>

Any help would be much appreciated. Thank you for reading.

Related posts

Leave a Reply

1 comment

  1. Try this: (taken from this post: WordPress function to get top level category of a post?):

    put this in your functions.php file at the bottom before the closing ?> tag

    function get_top_category() {
        $cats = get_the_category(); // category object
        $top_cat_obj = array();
    
        foreach($cats as $cat) {
            if ($cat->parent == 0) {
                $top_cat_obj[] = $cat;  
            }
        }
        $top_cat_obj = $top_cat_obj[0];
        return $top_cat_obj;
    }
    

    Amend your content.php:

    <?php $top_cat = get_top_category();?>
    
    <?php printf( __( 'Posted in %1$s', 'underground_sound' ), $top_cat->slug .' - '.$categories_list ); ?>