我找到了一个有效的解决方案!代码可以改进,但这只是一个开始。
把这个放进去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; ?>