Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

redirect the main category to a sub-category WordPress

  • SOLVED

I list in my navigation the main News category with a drop-down which lists 3 sub-categories:
<?php wp_list_categories('orderby=order&exclude=1,3&title_li=&depth=2'); ?>

I would like, when somebody clicks on the main one, to be taken to the first sub-category:
http://domanin.tld/category/news/
to
http://domanin.tld/category/news/in-the-news/

I have tried this with .htaccess permanent redirect, but is messing up the categories.

Any solutions?

L.E
Th Redirect plugin is not an option as it screws something else and I want to avoid it.
The custom menu from 3.0 is not a good solution as the functionality is more complex and I need to output current_cat class.

Answers (4)

2010-10-23

Cosmin Popovici answers:

Why don't you use the WordPress 3 menus and have the main category link as a custom link? You could then link it to where ever you need.

More info on WP3 nav menus:

[[LINK href="http://codex.wordpress.org/Appearance_Menus_SubPanel"]]http://codex.wordpress.org/Appearance_Menus_SubPanel[[/LINK]]

and especially here:
[[LINK href=" http://codex.wordpress.org/Navigation_Menus"]]
http://codex.wordpress.org/Navigation_Menus[[/LINK]]

<strong>EDIT reply: custom menus DO add a current class to menu items, try it and you'll see.

Also, you can add classes yourself. Just hit Screen Options when on the Menus admin page, and check "Classes" under "Show advanced menu properties"</strong>


Lucian Florian comments:

Cosim, I tried that just before I posted here. How do you link the main category to the subcategory. If I do that with a custom link, it won't output the post_ancestor class that I need for highlighting.


Cosmin Popovici comments:

You can add that class yourself for that custom link, just like I previously said :)


Lucian Florian comments:

Cosmin,
I know where you're heading, but that class will be visible all the time, even when I am on other pages. Any idea how it can be outputted only when on news section?


Cosmin Popovici comments:

Oh, you're right :)

Ok then, jQuery FTW .

Just create that custom link and let's assume you add a class of "parent" to it.

Then, in your footer.php, add this before the closing body tag:

<?php
if (is_category('placecatnamehere')) { ?>
<script type="text/javascript">
$('.parent').addClass('active');
</script>
<?php }
?>


Or, you could do the same, but specifying CSS directly:

Add "activeclass" to your custom link in the WP dashboard.

Then, before the closing head tag in your header.php, add this:

<?php
if (is_category('placecatnamehere')) { ?>
<style type="text/css">
.activeclass { /*define your styles here*/; }
</style>
<?php }
?>


Hope it all works out for you this way :)


Lucian Florian comments:

I've got it working. I didn't want to use jQuery for this and I could use an older code:

<?php $nav_menu = wp_nav_menu( array('menu' => 'Main Menu', 'menu_id' => 'nav', 'container' => ''));
// get post type
$post_type = get_post_type( $post );
if (preg_match('/news-class/', $nav_menu) && $post_type == 'news') {
$nav_menu = preg_replace('/class=\"news-class/', '/class="current_page_item /', $nav_menu);
}
echo $nav_menu; ?>

2010-10-23

Pippin Williamson answers:

Use this Redirect plugin. It works really well.

[[LINK href="http://wordpress.org/extend/plugins/redirect/"]]http://wordpress.org/extend/plugins/redirect/[[/LINK]]

2010-10-23

Utkarsh Kukreti answers:

Try
<?php

add_action( 'template_redirect', 'redirect_news_category' );
function redirect_news_category()
{
if( is_category( 'news' ) )
wp_redirect( get_category_link( get_cat_ID( 'in-the-news' ) ) );
}

?>


Lucian Florian comments:

I added that code in functions.php (without PHP tags) and nothing happens. Also tried to add that in my custom category template.


Utkarsh Kukreti comments:

Please try the code now.


Lucian Florian comments:


Getting bunch of errors now, with the wp_debug_on:

Notice: Undefined index: aiosp_enabled in /home2/peerthrn/public_html/wp-content/plugins/all-in-one-seo-pack/all_in_one_seo_pack.php on line 688

Notice: Undefined index: aiosp_enabled in /home2/peerthrn/public_html/wp-content/plugins/all-in-one-seo-pack/all_in_one_seo_pack.php on line 688

Notice: Use of undefined constant ddsg_language - assumed 'ddsg_language' in /home2/peerthrn/public_html/wp-content/plugins/sitemap-generator/sitemap-generator.php on line 44

Notice: Undefined index: page in /home2/peerthrn/public_html/wp-content/plugins/custom-field-template/custom-field-template.php on line 94

Notice: Undefined variable: post in /home2/peerthrn/public_html/wp-content/plugins/custom-field-template/custom-field-template.php on line 174

Notice: Trying to get property of non-object in /home2/peerthrn/public_html/wp-content/plugins/custom-field-template/custom-field-template.php on line 174

Notice: load_plugin_textdomain was called with an argument that is deprecated since version 2.7 with no alternative available. in /home2/peerthrn/public_html/wp-includes/functions.php on line 3323

Catchable fatal error: Object of class WP_Error could not be converted to string in /home2/peerthrn/public_html/wp-includes/pluggable.php on line 904


Lucian Florian comments:

I am getting rest of errors anyway, but original one for you code is this:
Catchable fatal error: Object of class WP_Error could not be converted to string in /home2/peerthrn/public_html/wp-includes/pluggable.php on line 904


Utkarsh Kukreti comments:

Does the redirect work with WP_DEBUG off?

Might be due to content being sent by other plugins' notices before the page could redirect.


Lucian Florian comments:

No, this is what I get with the wp_debug off: (blang page)

Catchable fatal error: Object of class WP_Error could not be converted to string in /home2/peerthrn/public_html/wp-includes/pluggable.php on line 904

2010-10-23

Joachim Kudish answers:

More or less the same as the above, you can do it manually by creating a template file category-news.php with the following contents:

<?php wp_redirect('/news/in-the-news/'); ?>


Lucian Florian comments:


I get this error if I just add that code.
Warning: Cannot modify header information - headers already sent by (output started at /home2/peerthrn/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:221) in /home2/peerthrn/public_html/wp-includes/pluggable.php on line 890


Joachim Kudish comments:

Try this instead (in the same category-news.php file)

<?php

if ( have_posts() ) : while ( have_posts() ) : the_post();

wp_redirect('/news/in-the-news/');

endwhile;
endif;


?>


Lucian Florian comments:

still getting errors


Joachim Kudish comments:

sounds like the error is coming from all-in-one-seo, can you disable it just for a minute to see if my solution works?

were you getting the same error with the second code?

cheers,
Joey