I know this is a favorite error! I understand the principles but I can't see why this code would create this error.

page.php includes a get_template_part.

I have a test page that renders and does a redirect. When it hits the redirect it gets the Headers Already Sent error. If I comment out a few lines in the template it all works but I'm not sure why the code causes the header error.

Any ideas why I get the headers error?

page.php looks like this

<?php get_header();?>
    <div class="wider-container">
        <?php $term = $wp_query->queried_object; ?>
        <section class="center-aligned inner-page-main">
           <aside id="align-right">
                <h3>Our Lists</h3>
                <ul id="our-lists">
                    <?php get_template_part( 'taxonomy-our-lists' );?>

The key part of taxonomy-our-lists.php looks like this.

foreach ( $categories as $category ) {
if ( $category->parent > 0 ) {
        echo get_option($category->name);
        echo '<li>';
        echo '<a', (($term == $category->slug) ? ' class="on"' : ''), ' href="' . 
            get_category_link( $category) . '" title="' . 
            sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . 
        echo '</li>';

If I comment out the last three echo's, not four, the error goes away.

  • 3
    Where is the redirect in your code? – fuxia Apr 13 '15 at 21:55
  • 1
    My guess is that you are calling the redirect after the echo's are called, and also that get_option($category->name); does not return anything. You can not have any output before calling the redirect. – czerspalace Apr 14 '15 at 0:55
  • To tell you the problem, you'd need to give an idea of exactly where the redirect occurs. It really should occur before any of what you have. It should be no later than the template_redirect action. If you have any output before the redirect, you are too late, so get_header or later is too late. – butlerblog Apr 14 '15 at 4:42
  • Got it, thanks for the comments, esp no output from get_option, that makes sense. Onward to slay the ugly dragon. – strattonn Apr 14 '15 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.