Lightning Rank - Private Blog Network

  • Home
  • Done 4 You PBN Service
  • Other Services
    • Amazon Associate Audit
    • Editorial Links
    • Buy Expired Domains
  • Contact
    • Affiliates

Display Single WordPress Post Category Even If Its A Sub Category

November 20, 2015 By Jon Gillham Leave a Comment

This tutorial will teach you how to display a single WordPress post category in your theme even if the article has been posted in a sub category. I created this technique for use in several WordPress themes I have developed where I only wanted to display the actual category that the article was posted in even when posted to sub categories. It’s a nice technique for themes with limited space or users that don’t want to display all categories that a post has been placed in.

Step 1: Creating A Retrieve Actual Category Function

Firstly you will want to open your current themes functions.php file then copy the code block below and paste it into your functions.php file at the bottom, close and save your functions.php file.

 //----------------------- [ Article Category Function ] ----------------------// // This function returns the category that the article is actually posted in even if in sub-category function article_category() { $category = get_the_category(); if ($category) { echo '<a title="' . sprintf( __( 'View all posts in %s', 'wpshock' ), $category[0]->name ) . '" href="' . get_category_link( $category[0]->term_id ) . '">' . $category[0]->name.'</a> '; } } 

Step 2: Using The Function Within Your WordPress Theme

Copy and paste the code below into your WordPress themes post loop function. You can search for the_category() function within your themes post loop and replace it with the code below in an existing WordPress theme.

<?php article_category(); ?>

If you have any improvements or questions about this code snippet please feel free to leave a comment.

Filed Under: Wordpress Tutorials Tagged With: navigation menus, post category, wordpress hack, wordpress tutorial

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • What It takes to get to #1 in Google
  • Private Blog Network Building Service in 2016 – Is It Still Worth The Risk?
  • UpThemes Scale Back On Theme Options Within Their WordPress Themes
  • Create A WordPress Login Form Shortcode For Your Blog
  • WooThemes Back Online After Malicious Hack & Further DDoS Attack

Our Team

Jon Gillham

Father, Husband and mechanical engineer. Jon has the easy job he builds and tests systems that Kelley, Hmd and their teams execute!
Ahmed Ali

Ahmed is the man that makes sure everything runs smoothly. A true jack of all trades he makes sure each site is built to our high standards!

Copyright © 2022 . Experts at Harnessing the Power of Expired Domains to Help You Outrank Your Competition