Support » Themes and Templates » single.php called for current and next post with pretty permalinks

  • Resolved 1991Syclone


    I seem to be having an issue with attempting to call a custom function only once from single.php. I have stripped it down to the very basics and have narrowed it down to pretty permalinks. Here is what I have done.

    1) Download and install WordPress 2.9.1
    2) Using the default theme, I modified single.php and included the following right after the get_header call:

    $handle = fopen("c:\\output.txt", "a+");
    fwrite($handle, "being called for post " . $post->ID . "\r\n");

    3) I added three generic posts.

    If I use default permalinks and view the first post, everything is fine. I get one line when I view the single post.

    If I use any of the other permalink formats, I get two lines in the output file. The first line is for the current post and the second line is for the next post.

    Shouldn’t this function only be called for the current post? Why is it being called for the current post and next post when pretty permalinks are enabled?

    Any help is greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’ve narrowed it down to the wp_head function (specifically adjacent_posts_rel_link). It looks like WordPress pre-loads the next post when this is enabled and therefore it will run any code you have stored in that particular file.

    This becomes a problem if you are attempting to do anything in PHP that should only be done for the post/page currently being viewed since the code will be run for subsequent posts.

    The solution for your functions.php file:

    remove_action( 'wp_head', 'adjacent_posts_rel_link');

    It would be nice if there was a way to determine if the page request was coming from this particular function so that code would not get executed in the target page.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘single.php called for current and next post with pretty permalinks’ is closed to new replies.