• 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!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Look in the WP_Query class:

    if ($wp_query->found_posts == 1)

    Thread Starter ASJInc

    (@asjinc)

    I appreciate the quick reply. Unfortunately it didn’t work and gives similar results to what I’m currently getting. Let me try to explain further.

    I have a parent category and some sub categories.

    Parent
    – child 1
    – post 1
    – child 2
    – post 2
    – child 3
    – post 3
    – child 4
    – post 4

    Each post has the category set as both the child category and the parent category so if I click on the Parent Category it will choose all of the articles in the child categories below it. What I need to do though is if I click on one of child categories it will forward to the post itself.

    I hope that helps to clarify.

    Thread Starter ASJInc

    (@asjinc)

    To further clarify…

    If I remove the Parent category as a cat for each post the script above works. Unfortunately, because then the Parent category doesn’t have any posts directly it follows to the first post of the child categories.

    Perhaps someone could help me with the logic to tell this to list out the posts of the child categories if the category has “0” (such as the parent)?

    I’ve just tried it on my test blog and it works for me. I added the following in my archive.php after <?php while (have_posts()) : the_post(); ?>:

    <?php
    	if ($wp_query->found_posts == 1) :
    		wp_redirect(get_permalink($post->ID)); exit;
    	endif;
    ?>

    If I clicked categories that only contain 1 post, it redirects straight to the post instead of the archive page.

    Thread Starter ASJInc

    (@asjinc)

    Thank you so very much Joseph! It worked like a charm. I must have messed it up the first time I tried your method.

    Thanks!!!

    You’re welcome.

    I really like this idea of yours. I might add this to my blog as well ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘If there is only one post in a category, redirect to that post.’ is closed to new replies.