0

I've created a super simple contact page template, just content and a contact form, like so:

<?php
/*
Template Name: Contact
*/
?>

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

    <div id="blockcontact">
        <?php the_content(); ?>
        <form id="contactform" method="post" action="<?php bloginfo('template_directory'); ?>/_assets/submitemail.php">
            <input name="user"  type="text" value="Name" />
            <input name="email" type="text" value="E-Mail"/>
            <input name="url" type="text" value="Website" />
            <input name="subject" type="text" value="Subject" />
            <textarea rows="10" name="message" cols="50" id="message">Your message here...</textarea>
            <input type="submit" class="button" value="SEND" id="send" />
            <div id="error"></div>
        </form>
    </div><!-- blockcontact -->

<?php endwhile; ?>

I then need to insert this page into another page (say, homepage) - using custom query. The query is irrelevant, I think, but it's basically getting all created pages and displaying their contents:

<?php 
    $temp_query = $wp_query;
    $args = array('post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_status' => 'publish', 'posts_per_page' => 5,);
    $wp_query = new WP_Query($args);
?>

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

    <div class="block group" id="<?php echo $post->post_name; ?>">

        <?php the_content(); ?>

    </div><!-- /<?php echo $post->post_name; ?> -->

<?php endwhile; ?>
<?php endif; ?>

<?php $wp_query = $temp_query; ?>

It seems to work fine for normal pages (those without a custom template), but for this contact page, I'm getting from the query is the content (the_content()) but not the form.

I also have another custom page template that I'm trying to pull into the homepage, and it's doing the same thing.

Any idea what would cause this problem? Never seen anything like that and I've been working with WordPress for years now...

Thanks in advance!

EDIT: I now see my problem - the_content() in my query works correctly - it pulls the entered content of the page. What I'm trying to achieve is get the entire content of the page, including my custom HTML, much like include() function in PHP. Would love some tips on how to do that.

3 Answers 3

1

Couldn't you use the custom form like this:

STEP ONE:

Open a Text editor and paste your form content-

<?php
// Custom Contact Form
?>
<div id="my-ccf">
<form id="contactform" method="post" action="<?php bloginfo('template_directory'); ?>/_assets/submitemail.php">
    <input name="user"  type="text" value="Name" />
    <input name="email" type="text" value="E-Mail"/>
    <input name="url" type="text" value="Website" />
    <input name="subject" type="text" value="Subject" />
    <textarea rows="10" name="message" cols="50" id="message">Your message here...</textarea>
    <input type="submit" class="button" value="SEND" id="send" />
    <div id="error"></div>
</form>
</div>

STEP 2: Save the file as my-contact-form.php (upload my-contact-form.php to your themes root directory)

STEP 3: When you want to display your contact form, place this in the template file:

<?php get_template_part('my-contact-form.php'); ?>
6
  • Problem is, it has to be a page. I also have a similar page with the_content() and a custom loop to display custom post type (portfolio items). Sounds more complicated than it is, I guess. I assume if I used a page template with <?php get_template_part('my-contact-form.php'); ?> inside it and then try to pull it via the_content() I would get the same thing - just the content entered in the editor?
    – Justine
    Commented Sep 3, 2011 at 18:34
  • 2
    @Justine - I think you have a fundamental misunderstanding of how templates work. The template is selected based on the request which generated a page. If you're on the homepage, the template associated with your homepage is loaded (font-page.php, index.php, etc..). If you want a specific piece of html output within a loop, you have to check within the loop if the current page being output is the contact page and explicitly include your separate html.
    – Milo
    Commented Sep 3, 2011 at 18:42
  • I'm not sure what you mean by "and then try to pull it via the_content()", If you use the get_template_part as suggested it will display the form. Commented Sep 3, 2011 at 18:46
  • @Milo - I see your point, I realized my mistake after posting the question, hence the edit. I understand that the_content() pulls just the content of the page, regardless of a template that it's using.
    – Justine
    Commented Sep 3, 2011 at 18:51
  • @Jeremy - It's really hard to explain, here's an example: link - each "section" of this single page theme - (service, portfolio, text etc.) is a page in WordPress. I need to the user to be able to add pages that will be displayed on this homepage.
    – Justine
    Commented Sep 3, 2011 at 18:53
0

If you need a form to be part of the content (rather than part of a page template), try using a shortcode to generate the form. (See the Shotcodes API documentation in the Codex). This way, you can include a shortcode as part of the content (ie it'll get returned from a regular post query), and you won't need to create a bunch of custom templates for use with one-off pages.

For example, you could put the following in your functions file (or in a plugin):

<?php
function wpsx_27632_my_shortcode_handler() {
?>
    <div id="my-ccf">
    <form id="contactform" method="post" action="<?php bloginfo('template_directory'); ?>/_assets/submitemail.php">
        <input name="user"  type="text" value="Name" />
        <input name="email" type="text" value="E-Mail"/>
        <input name="url" type="text" value="Website" />
        <input name="subject" type="text" value="Subject" />
        <textarea rows="10" name="message" cols="50" id="message">Your message here...</textarea>
        <input type="submit" class="button" value="SEND" id="send" />
        <div id="error"></div>
    </form>
    </div>
<?php
}
add_shortcode('my_contact_form', 'wpsx_27632_my_shortcode_handler'):
?>

And then, in the WYSIWYG, you can just add "[my_contact_form]" where you want the contact form to appear.

0

You need to create a proper custom page template.

A custom page template is intended to define all of the markup necessary to render a static page that has the template assigned. Your example code constitutes a template-part file, rather than a complete template file.

(Codex reference: Pages >> (Custom) Page Templates)

Creating a custom page template

So, first things first: you need to correct your code, by wrapping it all in get_header() and get_footer() calls and getting rid of the Loop, since it doesn't appear that you need it here. (You only need a Loop if you intend to display the_content() for the Static Page to which you assign the custom template, or if you intend to output a custom query loop.)

For example:

<?php get_header(); ?>

<div id="blockcontact">
    <?php the_content(); ?>
    <form id="contactform" method="post" action="<?php bloginfo('template_directory'); ?>/_assets/submitemail.php">
        <input name="user"  type="text" value="Name" />
        <input name="email" type="text" value="E-Mail"/>
        <input name="url" type="text" value="Website" />
        <input name="subject" type="text" value="Subject" />
        <textarea rows="10" name="message" cols="50" id="message">Your message here...</textarea>
        <input type="submit" class="button" value="SEND" id="send" />
        <div id="error"></div>
    </form>
</div><!-- blockcontact -->

<?php get_footer(); ?>

PROTIP: you want this template file to look exactly like page.php, except with the Loop replaced by your contact-form markup.

Using a custom page template

  1. Create a new static page; give it a title and no content
  2. Under Page Attributes, via the "Template" dropdown, assign the "Contact" template to the page
  3. Publish the page
  4. View the page, and enjoy your contact-form goodness

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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