Support » Themes and Templates » Post Thumb linking to the category

  • Hopefully this will be an easy one to sort out for you all.

    On the loop on the homepage I am just displaying the thumbs from the latest 4 posts. Instead of having a permalink to its post, I want it to take the user to the category its posted into.

    A strange request, I know, but any ideas?

    Many thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael


    Forum Moderator

    if you could post the code which shows the thumbnail (plus a few lines before and after), this might be a starting point to help.

    a general code to get the link to the first category of a post (in the loop)

    <?php $category = get_the_category();
    $cat_link = get_category_link( $category[0]->cat_ID ); ?>

    assuming that your code uses the_permalink() to link the thumb; replacing it with echo $cat_link; might work.

    Hi thanks for the help, the code is below. I’ve tried something similar to whats you’ve said, but it didn’t seem to work.

    <div id="latestframe">
    <?php if (have_posts()) : ?>
    <?php query_posts('posts_per_page=4'); ?>
    		<?php while (have_posts()) : the_post(); ?>
    <div class="latestboxes"><a href="<?php $category = get_the_category(); echo $category[0]->cat_slug; ?>"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" border="0" /></a></div>
    <?php endwhile; ?>
              <?php else : ?>
    		<h2>Not Found</h2>
    		<p>Sorry, no results where found.</p>
    	<?php endif; ?>

    Let me know what you think 😉



    Forum Moderator

    <div class="latestboxes"><a href="<?php $category = get_the_category(); echo $category[0]->cat_slug; ?>"><img...

    as you know, cat_slug is just the ‘clean’ version of the category name (lowercase letters, no special characters, joined with – ) and is not a link or path to the category archive page.

    you could try as suggested:
    <div class="latestboxes"><a href="<?php $category = get_the_category(); echo get_category_link( $category[0]->cat_ID ); ?>"><img...

    make a backup copy of your theme file before editing

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Post Thumb linking to the category’ is closed to new replies.