Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a page that loops posts. Each post acts as a category link so that when you click it brings you to a category archive.

My issue is that wordpress somehow overrides my htaccess and redirects me to the single.php post.

So ill have a category page called 'Neighborhoods'. Within 'Neighborhoods' I have a post called 'Highland Park'. When I click 'Highland Park' it brings you to the the single.php not the archive.php of 'Highland Park.'

Note: I uploaded my wordpress files into a folder called 'pages'. In my htaccess file I have a rewrite where 'neighborhoods/' is 'pages/category/neighborhoods/'

Is there a solution? Am I overlooking something?

Thanks in advance!

share|improve this question
add comment

2 Answers

Well, actually Wordpress provides wp redirect function. And here is a small plugin I've used a while back

<?php
$redirect_meta_key = 'redirect_to';

add_action('template_redirect', 'redirect_this');

function redirect_this($nothing)
{
    global $wp_query, $redirect_meta_key;

    if(is_single() || is_page()) {
        $redirect = get_post_meta($wp_query->post->ID, $redirect_meta_key, true);
        if('' != $redirect) {
            wp_redirect($redirect);
            header("Status: 302");
            exit;
        }
    }
}

?>

No all you have to do is to it in a custom field redirect_to with a value of absolute path to the other page, or alternatively, add a custom meta box

share|improve this answer
add comment

I'm using a generic redirect solution in my templates that uses custom fields. In my functions.php I have this function:

if (!function_exists('sumo_redirect')):
/**
 * Put this at the very top of a page and give it the current page id. It then
 * checks if there is a redirect_to custom field with another page id that it
 * can redirect to.
 *
 * @param    int    $post_id    The current page.
 */
function sumo_redirect($post_id)
{
    $redirect_to_meta = get_post_meta($post_id, 'redirect', TRUE);
    if (strlen($redirect_to_meta) > 0)
    {
        $redirect_to = (int) $redirect_to_meta;

        header( "HTTP/1.1 302 Found" );
        header( "Location: " . get_permalink($redirect_to) );
        die();
    }
}
endif;

And in my page template (or in your case the post template; single.php) I have this at the very top of the file:

<?php sumo_redirect($post->ID) ?>

Now you can edit your post and add to it a custom field named redirect, enter a post id and it will redirect to that. You might want to alter the function to also accept strings as the redirect destination (or even something more advanced like category:12, post:34, url:http://google.com etc).

share|improve this answer
2  
put die() or exit() after header, because in some cases the current script will continue to run... –  onetrickpony Jun 22 '11 at 5:45
    
Good catch, thank you! –  maryisdead Jun 25 '11 at 14:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.