4

My loop below shows the latest 4 posts from the same category as the post currently being viewed. Its located within single.php.

I'm trying to get the URL of that same category so I can link back to category.php to view all posts from that same category. I thought grabbing the category slug would work but my code below doesn't output anything:

<?php
global $post;
$categories = get_the_category();

    foreach ($categories as $category) :

       $exclude = get_the_ID();
       $posts = get_posts('posts_per_page=4&category='. $category->term_id);

        foreach($posts as $post) :
         if( $exclude != get_the_ID() ) { ?>

                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post c-1"> Link to actual post</a>

    <?php } endforeach; ?>

<a href="<?php bloginfo('url'); ?>/categories/<?php echo $childcat->cat_slug; ?>" title="View all" class="btn border"><i class="i-right-double-arrow"></i> View all <?php echo $childcat->cat_slug; ?></a>
<?php  endforeach; wp_reset_postdata(); ?>
8

Use:

get_category_link( $category_id );

See:

https://codex.wordpress.org/Function_Reference/get_category_link

In your specific case:

<?php
global $post;
$categories = get_the_category();

    foreach ($categories as $category) :

       $exclude = get_the_ID();
       $posts = get_posts('posts_per_page=4&category='. $category->term_id);

        foreach($posts as $post) :
         if( $exclude != get_the_ID() ) { ?>

                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post c-1"> Link to actual post</a>

    <?php } endforeach; ?>

<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" title="View all" class="btn border"><i class="i-right-double-arrow"></i> View all <?php echo $category->name; ?></a>
<?php  endforeach; wp_reset_postdata(); ?>
0

A STRAIGHT FORWARD AND CLEAN CODE

I'm a newbie :)

I modified the codes given by Adam and removed the unnecessary parts not needed to answer the initial question.

It worked for me 100%.

Give it a try.

Please let me know if it worked for you too :)

<?php $categories = get_the_category();
foreach ($categories as $category) :
endforeach; ?>

<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
  LINK TO CURRENT POST CATEGORY >>
</a>

OR

<?php $categories = get_the_category();
foreach ($categories as $category) : ?>

The category is:
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
  <?php echo $category->name; ?>
</a>

<?php endforeach; ?>

COMBINE SOME OTHER ELEMENTS AS YOU LIKE. PLAY AROUND BUDDY.

  • 1
    I don't understand the empty foreach. How does that work? Does that just select the last entry in $categories as $category, so you're only showing the link for the last category? – Rup Nov 26 '20 at 11:10
  • Looks like foreach is compulsory. Without it, the code will not work properly. You can use any thing as the term. You can decide to change to $category = get_the_category(); foreach ($category as $category) : I hope this helps. Don't forget, I am a newbie. Just playing around with stuffs. If you noticed, I removed one irrelevant line of code. Have fun. – Mrbiizy Nov 27 '20 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.