Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Sirin Srikier
Sirin Srikier
7,671 Points

How do I add a row in the default page template?

I am trying to put a form and background image in a row on all the pages so I can add them as html tags on the default page template but how can I do this in a cleaner way? I'm thinking adding this into a php file and include it on the default template but how do I do it?

1 Answer

Sue Dough
Sue Dough
35,800 Points

In programming you want to keep your code DRY. ( DO NOT REPEAT YOURSELF )

Since you mentioned this going on "all the pages" then you could use the get_template_part WordPress function.

First create a folder in your theme called "content". Then you can add the following file:

content-test.php

with the following code:

<div class="row">
  <form class="" action="index.html" method="post"></form>
  <?php _e( 'hello world', 'test' ); ?>
</div>

Then in your templates simply just put:

<?php get_template_part( 'content/content', 'test' ); ?>

This way you write 1 piece of code and can include it wherever in your theme many times as you want.

Alternatively if you use the Header or Footer you can include something on every page however that may not be ideal so get_template_part comes in very handy for this sort of stuff.