If there is only one post in a category, redirect to that post.
-
I’ve edited my category page to list out any post from a selected category, which I suppose is normal. To simplify this page, I would like that if there is a single post in a given category that the category page forwards to that single post. I’ve researched this already and found this solution, which I’ve tried to implement:
<?php if(have_posts()) : $category = get_the_category(); if ($category[0]->category_count == 1) : while (have_posts()) : the_post(); wp_redirect(get_permalink($post->ID)); endwhile; endif; endif; ?>
which is mentioned in this thread: https://www.ads-software.com/support/topic/go-to-post-from-a-category-link
This would work great, however each of my posts have two categories (a parent and child) so it will not work properly. Is there a way to count the posts of each category, see if there is one post, and then redirect this page to that post?
The above script seems to count the number of categories, not posts.
Any ideas? Thanks!
- The topic ‘If there is only one post in a category, redirect to that post.’ is closed to new replies.