Skip to content
This repository

the loop on single.php and page.php appears unnecessary. #301

Closed
drrobotnik opened this Issue · 6 comments

4 participants

Brandon K.Adam White Konstantin Obenland Michael Fields
Brandon

Is it for consistency that we include a while conditional around the loop on single/page.php?

https://github.com/Automattic/_s/blob/master/single.php#L13

If there is no post than they'd be redirected to the 404.php so to me it seems unnecessary. Also on a single/page there shouldn't be instances of more than one post without a custom query. I'd change this to an if conditional or remove it entirely like so:

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package _s
 */

get_header(); the_post(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <?php get_template_part( 'content', 'single' ); ?>

            <?php _s_content_nav( 'nav-below' ); ?>

            <?php
                // If comments are open or we have at least one comment, load up the comment template
                if ( comments_open() || '0' != get_comments_number() )
                    comments_template();
            ?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Curious on the thought behind the current decision before I submitted a PR.

K.Adam White

have_posts is useful for more than just a conditional check—per the codex:

As a side effect, have_posts starts, steps through, or resets The Loop. At the end of the loop, have_posts returns 0 after calling rewind_posts.

K.Adam White

Hmm, looking at the code behind have_posts, it's actually a fairly straightforward function... I'd say there's value in consistency, though.

Konstantin Obenland
Collaborator

I'm always in favor of removing code where it makes sense. Looking at have_posts(), and the lack of an else statement in the template make me wonder if this could be one of those cases.

Michael Fields

I prefer to use the standard Loop in all singular templates in _s. The code suggested above is what we refer to as a "partial loop" and really causes issues in other queries on the page. Historically this has appeared in a couple of the Default themes and has been fixed right away. Please see the following ticket: https://core.trac.wordpress.org/ticket/18794

Konstantin Obenland
Collaborator

Sweet, thanks for the heads up @mfields!

Michael Fields

No Problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.