Title: php code to link a category
Last modified: August 19, 2016

---

# php code to link a category

 *  Resolved [coggia](https://wordpress.org/support/users/coggia/)
 * (@coggia)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-code-to-link-a-category-1/)
 * Hello
 * I’m trying to make a dynamical link to a categorie but i guess i’m doing something
   wrong,
 * My posts usually got 2 categories : a ‘Support’ and a ‘Genre’
    and ‘Genre’ got
   sub categories..
 * I was trying to get a display like this:
    “Details : genre” and when you clicked
   on the genre it’ll link you to the posts with the similar genre
 * At first I wrote in the single.php:
    <p>Details : <?php $category = get_the_category();?
   >cat_ID; ?>”><?php echo $categories[1]->cat_name; ?>
   </p>
 * But if it worked well in some case, in others it doesn’t :
    let’s say for a post
   my categories are : Nes, Strategy It’s ok as the $category[1] will be strategy
 * But if i Got : Beat’em all, Nes
    the category[1] will be Nes, because the cats
   in WP are listed in alphabetical order
 * So how can I say to link to the “Genre” and its sub categories ?

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-code-to-link-a-category-1/#post-1453859)
 * This will have the answer:
    [http://wordpress.org/support/topic/384353?replies=2](http://wordpress.org/support/topic/384353?replies=2)
 *  Thread Starter [coggia](https://wordpress.org/support/users/coggia/)
 * (@coggia)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-code-to-link-a-category-1/#post-1453863)
 * err…well, not really;
    I guess I’m not explaining my problem quit right,
 * I’ve seen your post before posting mine, yours list all the categories and shows
   their attributes, my problem is I’m stuck with choosing one particular category
   which is included in another one, i make an example :
 * My Post is in 2Categories: NES, and GENRE->STRATEGY
 * I want to display this line in my single.php:
    that this post is STRATEGY and
   the link to al the posts that belong to STRATEGY
 * I apologize if the answer to this was in your post too, but i didn’t see how 
   🙁
 *  Thread Starter [coggia](https://wordpress.org/support/users/coggia/)
 * (@coggia)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-code-to-link-a-category-1/#post-1453865)
 * So far I only managed to get the link to all tthe posts belonging to GENRE with
   this:
 *     ```
       $category_id = get_cat_ID( 'Genre' );
       $category_link = get_category_link( $category_id );
       ```
   
 * But cannot found to which genre belong this particular post
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-code-to-link-a-category-1/#post-1453869)
 * If you want all the children of a category you can use the `child_of` argument
   with [Function_Reference/get_categories](http://codex.wordpress.org/Function_Reference/get_categories).
 *  Thread Starter [coggia](https://wordpress.org/support/users/coggia/)
 * (@coggia)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/php-code-to-link-a-category-1/#post-1453884)
 * Okay, thanks to you I managed to a solution 🙂
 * Maybe not an exceptionnal one, but it works, here it is if someone needs it one
   day:
 *     ```
       $catpost = get_the_category();
         foreach ($catpost as $cat) {
        	if($cat->category_parent==25){
       		$namecat= $cat->cat_name;
       		echo $namecat;
       		}
       	}
       ```
   
 * (the category parent ‘GENRE’ got the id = 25)
 * Thanks again

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

The topic ‘php code to link a category’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [coggia](https://wordpress.org/support/users/coggia/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/php-code-to-link-a-category-1/#post-1453884)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
