Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a template for a custom page type that will put the same data on every page of this type. The only problem is I see examples of custom page types but only using loops. Do I have to use a loop if it is a single page. How will I fill in the data here like title and content.

<?php
/*
Template Name: Use Case Template
*/
?>
<?php get_header(); ?>

<div class="wrap">

    <div class="leftcol">

        <h3 class="usehd">
            TITLE
        </h3>

        <div class="hd-div">

        </div>
        <div class="use-content">
            CONTENT
        </div>

    </div>

    <div class="rightcol">

        <div class="sideform">

            <form name="usecase" id="usecase">

                <label>Email</label><br>

                <input type="text" name="email" class="txtinput" />

                <br>

                <label>Full Name</label><br>

                <input type="text" name="fullname" class="txtinput" />

                <br>

                <label>Company Name</label><br>

                <input type="text" name="company" class="txtinput" />

                <br>

                <label>Number of employees</label><br>

                <select name="employeenum" class="useselect">

                    <option value="10">10</option>

                    <option value="100">100</option>

                    <option value="1000">1000</option>

                    <option value="9999">1000+</option>

                </select>

                <br>

                <input type="submit" value="Submit" class="usecasesubmit" />

            </form>

        </div>

    </div>

</div>
   <?php get_footer(); ?>
share|improve this question

3 Answers 3

up vote 0 down vote accepted

Yes, even for custom page templates, you will need to have a loop in order to get the title and content.

Try the following:

<?php
/*
Template Name: Use Case Template
*/
?>
<?php get_header(); ?>

<div class="wrap">
<?php if(have_posts()): while(have_posts()): the_post; ?>
<div class="leftcol">

    <h3 class="usehd">
        <?php the_title(); ?>
    </h3>

    <div class="hd-div">

    </div>
    <div class="use-content">
        <?php the_content(); ?>
    </div>

</div>

<div class="rightcol">

    <div class="sideform">

        <form name="usecase" id="usecase">

            <label>Email</label><br>

            <input type="text" name="email" class="txtinput" />

            <br>

            <label>Full Name</label><br>

            <input type="text" name="fullname" class="txtinput" />

            <br>

            <label>Company Name</label><br>

            <input type="text" name="company" class="txtinput" />

            <br>

            <label>Number of employees</label><br>

            <select name="employeenum" class="useselect">

                <option value="10">10</option>

                <option value="100">100</option>

                <option value="1000">1000</option>

                <option value="9999">1000+</option>

            </select>

            <br>

            <input type="submit" value="Submit" class="usecasesubmit" />

        </form>

    </div>

</div>
<?php endforeach; endif; ?>
</div>
<?php get_footer(); ?>
share|improve this answer
    
You could also end the foreach and if statements right after the end of the "leftcol" div. –  Kevin Langley Jr. Nov 7 '11 at 21:49

The reason while templates usually involve loop, is that even in sace of single post template - that is still technically a loop with single post.

You should really just study existing template, for example that of Twenty Eleven, and apply same logic in yours.

share|improve this answer

Why not just save the custom content as a separate file.

Example:

Using your code:

<div class="wrap">
  <div class="leftcol">
    <h3 class="usehd">TITLE</h3>
    <div class="hd-div"></div>
    <div class="use-content">CONTENT</div>
  </div>
  <div class="rightcol">
    <div class="sideform">
      <form name="usecase" id="usecase">
        <label>Email</label><br />
          <input type="text" name="email" class="txtinput" />
          <br />
        <label>Full Name</label><br />
          <input type="text" name="fullname" class="txtinput" />
          <br />
        <label>Company Name</label><br />
          <input type="text" name="company" class="txtinput" />
          <br />
        <label>Number of employees</label><br />
        <select name="employeenum" class="useselect">
          <option value="10">10</option>
          <option value="100">100</option>
          <option value="1000">1000</option>
          <option value="9999">1000+</option>
        </select>
        <br />
        <input type="submit" value="Submit" class="usecasesubmit" />
      </form>
    </div>
  </div>
</div>

You could save that as form.php

Then in your custom template just use this:

<?php
/*
Template Name: Use Case Template
*/
get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>  
  <?php the_content(); get_template_part('form') ?>
  <?php endwhile; else : endif; ?>  
<?php get_footer(); ?>

Using the code get_template_part calls the form.php file into the template file. This way you could use a conditional statement to include the form if it is the custom post type. You wouldn't need the custom template if you use the conditional statement to call the form.php from an existing template.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.