Hi,
Is there an easy way to let WP jump directly to the first child category within a catagory.
EX:
- References
- Trades
- Corporate events
So if you click on 'References' it should jump directly to 'Trades' (first child category within references).
Preview: http://www.aspbluesquare.be/en/category/references-en/
Thanks for the quick response.
rilwis answers:
Try this code:
add_action('get_header', 'redirect_to_child', 5);
function redirect_to_child() {
$cat = get_query_var('cat');
if (!$cat) return;
$child = get_categories(array(
'child_of' => $cat,
'number' => 1
));
if (empty($child)) return;
$child = $child[0];
wp_redirect(get_category_link($child->cat_ID));
}
Filip Van Reeth comments:
Hi,
Where do i have to paste this?
Does this work for any category?
rilwis comments:
Just put this code in functions.php file of your theme. This code works for all categories, which have children.
Filip Van Reeth comments:
it works but when I click on a reference to go to the single.php page it keeps coming back to the current category instead of going to the reference page.
Any way how to fix this.
Filip Van Reeth comments:
Hi you were almost there with the solution?
Can you give it another try please.
Thanks for the quick response.
rilwis comments:
Oh, I forgot to check if current page is category. Just added it in functions.php file (w/ access you gave me). Here's the completed code:
add_action('get_header', 'redirect_to_child', 5);
function redirect_to_child() {
if (!is_category()) return;
$cat = get_query_var('cat');
if (!$cat) return;
$child = get_categories(array(
'child_of' => $cat,
'number' => 1
));
if (empty($child)) return;
$child = $child[0];
wp_redirect(get_category_link($child->cat_ID));
}
Filip Van Reeth comments:
Thanks, works like a charm ;-)
Lucas Wynne answers:
Personally I would use this plugin:
[[LINK href="http://wordpress.org/extend/plugins/quick-pagepost-redirect-plugin/screenshots/"]]http://wordpress.org/extend/plugins/quick-pagepost-redirect-plugin/screenshots/[[/LINK]]
Sébastien | French WordpressDesigner answers:
try the code of rilwis with exit :
add_action('get_header', 'redirect_to_child', 5);
function redirect_to_child() {
$cat = get_query_var('cat');
if (!$cat) return;
$child = get_categories(array(
'child_of' => $cat,
'number' => 1
));
if (empty($child)) return;
$child = $child[0];
wp_redirect(get_category_link($child->cat_ID));
exit;
}