IF statements & archive displays - working locally but not live

OK this is driving me a liitle crazy.

I have a blog, plus 2 CPTs ‘news’ and ‘newsletters’.

I have a standard page ‘latest-news’ with a loop showing the ‘news’ and ‘newsletters’ posts.

I have a sidebar.php with lots of if statements basically saying:

  • if is blog or single (but not ‘news’ or ‘newsletters’) then show blog archive
  • if is ‘latest-news’ then show ‘news’ archive and ‘newsletters’ archive.

This works perfectly on localhost… But when I upload to a test domain to show the client the

  • if is ‘latest-news’ then show ‘news’ archive and ‘newsletters’ archive

part breaks and only shows blog archive posts.

I cant see whats going wrong, Ive tried switching databases, deleting and reuploading the theme, etc. But nothing works.

I’ll post the sidebar code below but im prettty sure its not wrong because its working locally!

<nav>

    <?php

    /* -----------------------------------------------------------------------------------------------

        IF is index or archive page
        but NOT testimonials / travelalerts / latestnews / newsletters

    ----------------------------------------------------------------------------------------------- */

        if ( is_home() || is_archive() && ! is_post_type_archive( array ('testimonials', 'travelalerts', 'latestnews', 'newsletters')) ) :
            
            echo "<h4 class='hide show-md'>Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

    /* -----------------------------------------------------------------------------------------------

        ELSE IF is single post and IS category blog 

    ----------------------------------------------------------------------------------------------- */

        elseif ( is_single() && in_category('blog') ) : 
            echo "<h4 class='hide show-md'>Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

    /* -----------------------------------------------------------------------------------------------

        ELSE IF is archive page latestnews

    ----------------------------------------------------------------------------------------------- */

        elseif ( is_post_type_archive('latestnews')) :

            echo "<h4 class='hide show-md'>News Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('post_type=latestnews&type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('post_type=latestnews&type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'latestnews' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

            echo "<h4 class='hide show-md'>Newsletter Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('post_type=newsletters&type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('post_type=newsletters&type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'newsletters' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

    /* -----------------------------------------------------------------------------------------------

        ELSE IF is single post latestnews

    ----------------------------------------------------------------------------------------------- */

        elseif ( is_singular('latestnews') ) :
            
            echo "<h4 class='hide show-md'>News Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('post_type=latestnews&type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('post_type=latestnews&type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'latestnews' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

            echo "<h4 class='hide show-md'>Newsletter Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('post_type=newsletters&type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('post_type=newsletters&type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'newsletters' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

    /* -----------------------------------------------------------------------------------------------

        ELSE IF is archive page newsletters

    ----------------------------------------------------------------------------------------------- */

        elseif ( is_post_type_archive('newsletters')) :

            echo "<h4 class='hide show-md'>News Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('post_type=latestnews&type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('post_type=latestnews&type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'latestnews' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

            echo "<h4 class='hide show-md'>Newsletter Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('post_type=newsletters&type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('post_type=newsletters&type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'newsletters' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

    /* -----------------------------------------------------------------------------------------------

        ELSE IF is single page latest-news

    ----------------------------------------------------------------------------------------------- */

        elseif ( is_page('latest-news')) :

            echo "<h4 class='hide show-md'>News Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('post_type=latestnews&type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('post_type=latestnews&type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'latestnews' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

            echo "<h4 class='hide show-md'>Newsletter Archives</h4>";

            echo "<ul class='hide show-md'>";
                wp_get_archives('post_type=newsletters&type=yearly&limit=1');

                echo "<ul>";
                    wp_get_archives('post_type=newsletters&type=monthly&limit='.date('m').'&show_post_count=1');
                echo "</ul>";

                $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'newsletters' ORDER BY post_date DESC");
                unset($years['0']);
                foreach($years as $year) :

                    echo "<li><a href='";

                        echo get_year_link($year);

                    echo "'>";

                        echo $year;
                    echo "</a></li>";

                endforeach;

            echo "</ul>";

    /* -----------------------------------------------------------------------------------------------

        END

    ----------------------------------------------------------------------------------------------- */

        else :
        endif;
    ?>
</nav>

Dude, what’s with all the echo statements? The readability of this code is unacceptable. Refactor it and provide something that’s readable please.

Here’s a start:

<?php if (is_home() || is_archive() && !is_post_type_archive(['testimonials', 'travelalerts', 'latestnews', 'newsletters'])) : ?>
  <h4 class="hide show-md">Archives</h4>
  <ul class="hide show-md">
    <?php wp_get_archives('type=yearly&limit=1'); ?>
1 Like