WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Go to post from a category link ? (11 posts)

  1. Ralev
    Member
    Posted 4 years ago #

    Hello everyone.
    I didn't find any similar topic, so I'm opening a new one.

    Here's the situation:
    I'm making a site with products and I've put all the products as posts in order to use the advantage of the category sorting and the tag system later.
    It appears that it's more convenient to have a subcategory with the name of the post/product in order to be visible on mouseOver from the central menu. But In this case a category opens and you still have to click over the post title to access the post itself.

    1 Products > 2 category > 3 category with product name > 4 post with product.

    So I want to find a plugin or inject a script that says something like:
    "if there's only one post in (3) - redirect straight to (4) and skip one click for the user".

    Something like a bypass.. or whatever you want to call it :)
    Help is very appreciated :)

  2. equalmark
    Member
    Posted 4 years ago #

    I can't really understand why you need to sub category. The category page can list all the posts in that category for you to click on?

  3. Ralev
    Member
    Posted 4 years ago #

    Okey, I understand it's essential to be understood in order to be helped :) so I'm gonna explain more in detail...

    If you don't put the post in a subcategory with the same name - there's no way to see its name in the main drop-down menu.

    So this way I'm exposing the existence of the product without making the user to search it anywhere. It's just visible from the first mouseOver.

    I guess there's a way to make some very complex menu script.... but at this point it seems to me more easier to find an elegant solution to bypass the category subpage if there's only one post in it.

  4. Stian Andreassen
    Member
    Posted 4 years ago #

    Create a category.php in your theme folder, with this code:

    <?php
    	if(have_posts()) :
    		while (have_posts()) : the_post();
    		wp_redirect(get_permalink($post->ID));
    		endwhile;
    	endif;
    ?>

    If you just want it for certain categories (so as not to fudge up your other category listings), name it category-x.php where x is the ID of the category. If you're running WordPress 2.9, you can also name it category-{category-slug}.php

  5. Ralev
    Member
    Posted 4 years ago #

    Thank you, stiand!
    The redirect script works just fine.
    The thing is that now every time I want to make a new product / or my friend, the client... will have to make such a page and that's why I want more automatic solution.

    I'll read a few more days.. and then I think I'm gonna copy-paste all 40 products as "pages"... since this looks like the most easy way.

    Then... I'll have to figure out a way for making their slug automatically skip the category and the child category word and produce simple address like : http://site.com/product1

    I still believe more in the potential of the post for better choice for the product pages so how can I expand the script you gave me in order to make it like:

    ""if there's only one post in category (3) - redirect straight to post(4)"

    1 Products > 2 category > 3 category with product name > 4 post with product.

  6. Ralev
    Member
    Posted 4 years ago #

    Here's a solution everyone can use >>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    What I've done is to make second category plugin.

    <?php /*
         Category Template: skip-to-post
         Template URI: http://ralev.com/
         Description: redirects to the first post in cat
         */ ?>
    <?php
    	if(have_posts()) :
    		while (have_posts()) : the_post();
    		wp_redirect(get_permalink($post->ID));
    		endwhile;
    	endif;
    ?>

    and then call if with the plugin :
    "Idealien Category Enhancements"
    http://wordpress.org/extend/plugins/idealien-category-enhancements/

    Now for every created category with one post I can select from a drop-down menu ( in the category panel ) the secondary "skip" template :)
    Works like a charm with WP 2.9! :)

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    PS: if somebody write an automatic condition script that could be attached to the main category.php and checks if there's just one post in the category and redirects... then it will be even better, so we don't have to choose a template - it will decide automatically if it needs redirect :)

    PPS: thank you, "stiand" for your help! :)

  7. adayin
    Member
    Posted 4 years ago #

    Have yet to give Ralevs solution a go, but would love an automatic script/plugin. I've been searching for some thing like this for a while now.

  8. adayin
    Member
    Posted 4 years ago #

    I can't get Ralevs solution to work. If any one does know of a plugin, fucntion or snippet of code that will bypass the category page straight to the post if only one post in category, that would be great.

  9. mtonumaa
    Member
    Posted 4 years ago #

    stiand,

    i saved and uploaded your code as categories.php, but no redirect happens..

  10. Stian Andreassen
    Member
    Posted 4 years ago #

    mtonumaa,

    Filename should be category.php, not categories.php

  11. insistimento
    Member
    Posted 4 years ago #

    ralev,

    here is my solution: "an automatic condition script that could be attached to the main category.php and checks if there's just one post in the category and redirects."

    <?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;
    		else :
    			include(TEMPLATEPATH . '/index.php');
    		endif;
    	endif;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic