Richard
Richard

Reputation: 521

Do I need the tags 'html' and 'body' in a WordPress Page Template?

I'm creating a Page Template for a WordPress site.

In the file newpagetemplate.php I currently have this code, and this code only:

<html>
<body>
<?php
/*
Template Name: Salespage
*/
?>
<div>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php the_title(); ?>
        <?php the_content(); ?> 
    <?php endwhile; endif; ?>
</div>
</body>
</html>

I'll need to make amendments, to set values for margins, fonts etc.

In the above code, do I need the 'html' and 'body' tags?

(If I take those tags out, a page with this page template applied still displays ok.)

Upvotes: 1

Views: 1800

Answers (5)

Jezen Thomas
Jezen Thomas

Reputation: 13800

I don't know why you're receiving so many theoretical answers. That to me seems like an uninteresting waste of time.

The simple answer is No.

From the code you provided, it looks as though you're trying to build a custom Wordpress theme. You'll want to place the doctype and opening html/body tags in header.php, and close those tags in footer.php. You then pull in the header and footer templates from your ‘Salespage’ template. It may look a little something like this:

// header.php

<!DOCTYPE html>
<html>
    <head>
    <title>Your Title</title>

    <?php wp_head(); ?>
    </head>
<body>

// newpagetemplate.php

<?php get_header(); ?>

<div class="yourContent">
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <?php the_title(); ?>
            <?php the_content(); ?> 
        <?php endwhile; ?> 
    <?php endif; ?>
</div>

<?php get_footer(); ?>

// footer.php

<?php wp_footer(); ?>
</body>
</html>

Upvotes: 7

Abid Hussain
Abid Hussain

Reputation: 7762

try this

if you have use wordpress and create new template page

then you code is below

This is new page templates "Salespage"

        <div id="container" class="one-column">
            <div id="content">

            <?php


            /* Run the loop to output the page.
             * If you want to overload this in a child theme then include a file
             * called loop-page.php and that will be used instead.
             */

             get_template_part( 'loop', 'salespage' );
            ?>

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

<?php get_footer(); ?>

// and you loop page is loop-salespage.php

and your code is

<?php
/**
 * The loop that displays a page.
 *
 * The loop displays the posts and the post content.  See
 * http://codex.wordpress.org/The_Loop to understand it and
 * http://codex.wordpress.org/Template_Tags to understand
 * the tags used in it.
 *
 * This can be overridden in child themes with loop-page.php.
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.2
 */
?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php if ( is_front_page() ) { ?>
                        <h2 class="entry-title"><?php the_title(); ?></h2>
                    <?php } else { ?>
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    <?php } ?>

                    <div class="entry-content">
                        <?php the_content(); ?>
                        <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                        <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                    </div><!-- .entry-content -->
                </div><!-- #post-## -->

                <?php //comments_template( '', true ); ?>

<?php endwhile; // end of the loop. ?>

Upvotes: 0

Bonius
Bonius

Reputation: 121

If you're talking about a Page template and not a Site template, then you do not need to have the html or body tags within the page template itself since it will be wrapped within the Site template.

Upvotes: 2

Jim
Jim

Reputation: 73936

No. The opening and closing tags for the <html> and <body> element types are optional. If the browser doesn't see them, they are implied.

Here is the definition for the <html> element type. Note that both start and end tags are optional.

Here is the definition for the <body> element type. Note that both start and end tags are optional.

Upvotes: -2

Oliver Spryn
Oliver Spryn

Reputation: 17348

Yes, you still need the <html> and <body> tags, since your page is rendered as HTML. All valid HTML includes these tags. Also don't forget to add <!DOCTYPE html> at the top of the page, again for valid HTML, and to avoid triggering what is called quirksmode on newer browsers. This is usually triggered as a preventative measure due to poorly structured HTML, and often easy for a developer to correct. ;)

Upvotes: -1

Related Questions