WordPress.org

Ready to get started?Download WordPress

Forums

Post Thumb linking to the category (4 posts)

  1. kirkslater
    Member
    Posted 4 years ago #

    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

  2. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    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.

  3. kirkslater
    Member
    Posted 4 years ago #

    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; ?>
    
    </div>

    Let me know what you think ;)

  4. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    <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

Topic Closed

This topic has been closed to new replies.

About this Topic