admin管理员组

文章数量:1336563

A simple page.php template often looks like this:

<?php
/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */
get_header(); ?>

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

      <?php while ( have_posts() ) : the_post(); ?>
        <?php get_template_part( 'content', 'page' ); ?>
        <?php comments_template( ’, true ); ?>
      <?php endwhile; // end of the loop. ?>

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

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

So there is a while loop for post's inside.

But I do not have several "pages" rendered out in any case. Is what assume!

E.g. for archive's this loop makes totally sense.


Now to my question

I would like to structure the template like the following example:

<?php get_header(); ?>

<?php while ( have_posts() ) : the_post(); ?>
  <?php a_special_content_output_before_the_main_container(get_title()); ?>

  <div id="primary" class="site-content">
    <div id="content" role="main">
        <?php get_template_part( 'content', 'page' ); ?>
        <?php comments_template( ’, true ); ?>
    </div><!-- #content -->
  </div><!-- #primary -->
<?php endwhile; // end of the loop. ?>

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

This would mean if this while loop has two items inside I will get invalid markup, because of duplicated id selectors.

Is there a situation a page can have several entries. Means that have_posts() is bigger 1?

A simple page.php template often looks like this:

<?php
/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */
get_header(); ?>

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

      <?php while ( have_posts() ) : the_post(); ?>
        <?php get_template_part( 'content', 'page' ); ?>
        <?php comments_template( ’, true ); ?>
      <?php endwhile; // end of the loop. ?>

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

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

So there is a while loop for post's inside.

But I do not have several "pages" rendered out in any case. Is what assume!

E.g. for archive's this loop makes totally sense.


Now to my question

I would like to structure the template like the following example:

<?php get_header(); ?>

<?php while ( have_posts() ) : the_post(); ?>
  <?php a_special_content_output_before_the_main_container(get_title()); ?>

  <div id="primary" class="site-content">
    <div id="content" role="main">
        <?php get_template_part( 'content', 'page' ); ?>
        <?php comments_template( ’, true ); ?>
    </div><!-- #content -->
  </div><!-- #primary -->
<?php endwhile; // end of the loop. ?>

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

This would mean if this while loop has two items inside I will get invalid markup, because of duplicated id selectors.

Is there a situation a page can have several entries. Means that have_posts() is bigger 1?

Share Improve this question asked May 25, 2020 at 12:44 André KellingAndré Kelling 1,0061 gold badge10 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

On single page and post templates the main loop is only ever going to contain one post, so you don't need to worry about duplicate IDs in your example.

本文标签: Can i forget about the post loop inside the page template