Display the top level category in WordPress

By Tim Priebe on July 16, 2009

By Tim Priebe on July 16, 2009

The is just a quick WordPress code snippet for those WordPress programmers or code dabblers out there.

Recently we needed a page in WordPress to display the top parent page title rather than the current page’s title as the heading on the page. Here’s the code we used to accomplish that:

<?
$currPost = $post;
while ($currPost->post_parent) {
  $currPost = $currPost->post_parent;
}
$title = get_the_title($currPost);
?>

<h2>[ <?php echo $title; ?> ]</h2>

Ready to get started?

Ready to take your digital marketing to the next level? We're here to help. Let's talk.