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.

Is it possible to redirect from a permalink (a single article) to that same post in a relevant category page, which might be paginated?

So when people visit http://domain/post_number they get redirected to http://domain/category/page/2/#post_number?

share|improve this question
    
Could you explain a little about why you want to do this, as I'm struggling to understand. Surely doing this will mean that no one can every view a single post (making single.php redundant)? –  David Gard Mar 15 '13 at 16:29
    
Reasoning behind it is that posts from one specific category should not be viewable through single.php, but only through category.php. However I would also like to use FB like and Twitter buttons so I do need to have those posts to have a permalink. So when a post from that category gets posted to FB, people will follow the link to the permalink, but that should redirect to a category view. I'm struggling a bit to explain, but I hope you get what I'm driving at? –  Squrler Mar 15 '13 at 16:47
    
Another approach would be to disable pagination for that category so it would display all posts that were ever posted in that category, but over time that would lead to an extremely long category page. So that's not an option.. :( –  Squrler Mar 15 '13 at 16:50
    
I think so. I was thinking you could create single-cat.php (where 'cat' is the slug of the category you intend this for), and copy the code of category.php in to that, but it appears that it doesn't exist in the hierarchy. Perhaps worth having a look though, as you may find it useful for the future - Template Hierarchy Codex. –  David Gard Mar 15 '13 at 16:53
    
Yeah, unfortunately single-cat.php doesn't exist, as you mentioned. I suppose the trick is trying to figure out on which paginated category page that single would be displayed. So presumably, I could first do a count of the amount of posts that have been published within this category, and divide that by the posts per page variable, and that would be a way to construct the url to redirect to. –  Squrler Mar 15 '13 at 17:00
add comment

1 Answer 1

up vote 2 down vote accepted

I found a solution that works! Code can be improved, but this is a start.

Put this in single.php:

<?php /* 1. First check to see if single is in the category that is not allowed to be viewable through the single template: */  
if (in_category('in-het-kort') ) {

/* 2. Do a loop to determine how many posts there are in a certain category. */
$args=array(
    'cat'       => 1036,
    'showposts' => -1
);

$count=0;

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {

    while ($my_query->have_posts()) : $my_query->the_post();

        /*Find out what the offset is for the current post */
        $count++;
        if ( $current_post == get_the_ID() ) {
            $post_offset = $count;
        }

    endwhile;

} 
wp_reset_query();

?>

<?php /* 3. Then construct the URL to redirect to. */ ?>

<?php 
$postsperpage = get_option('posts_per_page');
$cat_page_number_notrounded = $post_offset / $postsperpage;
$cat_page_number = round($cat_page_number_notrounded, 0, PHP_ROUND_HALF_UP);
$redirect_url = get_bloginfo('url').'/categorie/in-het-kort/page/'.$cat_page_number.'#post_'.get_the_ID();
?>

<?php /* 4. Then redirect the user to this URL either via Javascript... */  ?>   
<script>
window.location = "<?php echo $redirect_url; ?>"
</script>

<?php /* 5. ...Or via PHP, depending on your needs. */ ?>
<?php wp_redirect($redirect_url); exit; ?>
share|improve this answer
    
Glad you got something sorted. Can I suggest though, that instead of using JS to redirect the user (won't work if they have disabled JS in their browser), that you use PHP - wp_redirect($redirect_url); exit;. –  David Gard Mar 18 '13 at 10:19
    
Excellent, updated the answer. Thanks! –  Squrler Mar 18 '13 at 11:12
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.