• Resolved coggia

    (@coggia)


    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)
  • Thread Starter coggia

    (@coggia)

    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

    (@coggia)

    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

    If you want all the children of a category you can use the child_of argument with Function_Reference/get_categories.

    Thread Starter coggia

    (@coggia)

    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.