Support » Themes and Templates » Multiple instances of category link code

Multiple instances of category link code

  • I’m trying to build a home page of a site whose organisation is based on categories.

    So I have three articles from each category – title, or titles and excerpts and I want a link to each category’s archive – with “see all the articles”” or something.

    I’m not good at PHP – I’m more of a copier and paster ! I found this in the codex :

    <?php $category_id = get_cat_ID( 'My Category' );
     $category_link = get_category_link( $category_id );?>
     <a href="<?php echo $category_link; ?>" title="My Category">See all the articles</a>

    This works perfectly with the first instance, but I get “Catchable error… for the second and any subsequent uses – I try changing the names of the variables, but that doesn’t change anything.

    Why is this happening, and what should I do so it will work ? Please keep explanations easy to understand for the PHP retarded !

Viewing 6 replies - 1 through 6 (of 6 total)
  • esmi


    Forum Moderator

    Where are you grabbing the category ids from?

    According to the codex, I put the category name, for ex. ‘my-info’ between the brackets, and the code does the rest !

    WordPress no longer seems to show the database ID number in admin, so I used what was suggested… In the end I hacked the code by going into phpadmin to find the number.

    Actually I think I have a way of doing it without fiddling with code…

    But I would like to know why it works once, and not the other times.



    Forum Moderator

    Are you resetting the query between each of these blocks? Have you looked at using get_posts instead – which might be more suitable for this kind of secondary query?

    How do you reset the query ? I tried doing something with “return”, but I don’t know anything about it, so everything crashed !

    I use get_posts() to get my three excerpts, but then, after, my little list, I want to give a link to that category’s page – that works fine. Maybe there’s a way to use get_posts() to render the “foreach” loop, and then give me the category page link, but I don’t know how. Told you I didn’t know much ! Pushings around with HTML and CSS I can cope with but getting stuff from the database defeats me !



    Forum Moderator

    I’ll try it !

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multiple instances of category link code’ is closed to new replies.