Title: Retrieve permalink category in loop
Last modified: August 21, 2016

---

# Retrieve permalink category in loop

 *  Resolved [mccreath](https://wordpress.org/support/users/mccreath/)
 * (@mccreath)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/retrieve-permalink-category-in-loop/)
 * Is there a way to tell in the loop which category has been selected as the permalink
   category? We’d like to be able to reflect that category as a link with the post.
   I think I can get it by parsing the permalink itself, but if there’s an easier
   way, it would be great.
 * [http://wordpress.org/plugins/wp-category-permalink/](http://wordpress.org/plugins/wp-category-permalink/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Thread Starter [mccreath](https://wordpress.org/support/users/mccreath/)
 * (@mccreath)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/retrieve-permalink-category-in-loop/#post-4516715)
 * Ah, figured it out. I didn’t realize the plug-in was creating a post_meta field.
   All good!
 *  Plugin Author [Jordy Meow](https://wordpress.org/support/users/tigroumeow/)
 * (@tigroumeow)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/retrieve-permalink-category-in-loop/#post-4516719)
 * I wondered if I should create a function to simply this ^^
 *  Thread Starter [mccreath](https://wordpress.org/support/users/mccreath/)
 * (@mccreath)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/retrieve-permalink-category-in-loop/#post-4516721)
 * It would be a nice-to-have. I figured it out once I took the time to read through
   your PHP. The requirement for the particular site that I’m working on is that
   one, and only one, category be displayed with each post on the home page and 
   archive pages. Most posts will only be in one category, but a certain number 
   will be in two and I wanted to have control over which one displayed.
 * Here’s what I ended up writing for my page:
 *     ```
       $cat_id = get_post_meta( $post->ID , '_category_permalink', true );
       if ( $cat_id != null ) {
           $cat = get_category( $cat_id );
           $category_link = get_category_link( $cat_id );
           $category_name = $cat->name;
       } else {
           $cat = get_the_category();
           $category_link = get_category_link( $category->term_id );
           $category_name = $cat[0]->cat_name;
       }
       echo '<a href="'.$category_link.'">'.$category_name.'</a>';
       ```
   
 * I also changed the action word to “Primary” instead of “Permalink” because that
   makes more sense for the usage that we have.
 *  Thread Starter [mccreath](https://wordpress.org/support/users/mccreath/)
 * (@mccreath)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/retrieve-permalink-category-in-loop/#post-4516722)
 * oops! double-post.
 *  [purrdesign](https://wordpress.org/support/users/purrdesign/)
 * (@purrdesign)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/retrieve-permalink-category-in-loop/#post-4516813)
 * Any way to modify this to output the primary category ID as well as the name?
   I’m needing to style posts differently based on which primary category they are
   in.
 * Thanks for the great plugin and for the trick to output the category within the
   loop as well.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Retrieve permalink category in loop’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/wp-category-permalink_161616.svg)
 * [WP Category Permalink](https://wordpress.org/plugins/wp-category-permalink/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-category-permalink/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-category-permalink/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-category-permalink/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-category-permalink/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-category-permalink/reviews/)

 * 5 replies
 * 3 participants
 * Last reply from: [purrdesign](https://wordpress.org/support/users/purrdesign/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/retrieve-permalink-category-in-loop/#post-4516813)
 * Status: resolved