Warning: Cannot modify header information

2019-01-29 12:18发布

问题:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/51/5126851/html/wp-includes/post-template.php:54) in /home/content/51/5126851/html/wp-includes/pluggable.php on line 890

I know I need to do something to my post-template.php file, but I'm not sure what. I looked at the other answers but mine seems a little different. Here's what the relevant function looks like:

/**
 * Display or retrieve the current post title with optional content.
 *
 * @since 0.71
 *
 * @param string $before Optional. Content to prepend to the title.
 * @param string $after Optional. Content to append to the title.
 * @param bool $echo Optional, default to true.Whether to display or return.
 * @return null|string Null on no title. String if $echo parameter is false.
 */
function the_title($before = '', $after = '', $echo = true) {
    $title = get_the_title();

    if ( strlen($title) == 0 )
        return;

    $title = $before . $title . $after;

    if ( $echo )
        echo $title; // <-- This is line 54 of post-template.php
    else
        return $title;
}

回答1:

My first recommendation would be to learn how to format code for SO. And figure out how to cut down your problem to the bare minimum someone needs to solve it.

My second would be to look around the line mentioned in the error. I just did, and look what I found:

if ( $echo )
    echo $title;

So now you know what's outputting stuff, what can you do about it?

Well, the other part of that statement is:

else
    return $title;

Now, I'm no Wordpress expert, but I'm sure that you can work out the first thing that needs changing.



回答2:

You shouldn't be editing files in wp-includes without a good reason and a good understanding of what you're doing. WordPress is extendable via themes and plugins in almost all situations - you should rarely, if ever, have to hack core code to do something.



标签: wordpress