WordPress.org

Ready to get started?Download WordPress

Forums

Multiple instances of category link code (7 posts)

  1. nick_nielsen
    Member
    Posted 4 years ago #

    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 !

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Where are you grabbing the category ids from?

  3. nick_nielsen
    Member
    Posted 4 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    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?

  5. nick_nielsen
    Member
    Posted 4 years ago #

    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 !

  6. esmi
    Forum Moderator
    Posted 4 years ago #

  7. nick_nielsen
    Member
    Posted 4 years ago #

    I'll try it !
    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic