0

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?

5
  • 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
    Commented Mar 15, 2013 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
    Commented Mar 15, 2013 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
    Commented Mar 15, 2013 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
    Commented Mar 15, 2013 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
    Commented Mar 15, 2013 at 17:00

1 Answer 1

3

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; ?>
1
  • 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
    Commented Mar 18, 2013 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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