WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Could somebody help me make a custom page template?

I'd like to have a basic page (with sidebars, header and footers) and a table for data. I'd like the data to be taken from custom fields. Something like this:

Name: (name key)

Location: (location key) etc...

share|improve this question

closed as not constructive by EAMann Feb 27 '12 at 15:22

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

What theme are you using? It is easiest to work of existing template so everything matches rest of the site. If it is publicly available please provide link. – Rarst Feb 17 '11 at 20:03
@Rarst It's Arras – Ambo100 Feb 17 '11 at 20:13
WordPress Answers is not RentACoderForFree. Read the Codex first and then come asking questions when you're stuck. – scribu Feb 17 '11 at 20:25
@scribu Fair enough, Where should I request help? – Ambo100 Feb 17 '11 at 20:27
Try jobs.wordpress.net or codepoet.com – scribu Feb 17 '11 at 20:28
up vote 4 down vote accepted

Ok, first step is to create a page template, this is the easy part, and there's no query manipulation needed here, you're not changing the query, simply looking at the page that's already in the query, so that's a simple case of creating a basic loop, like so...(noting that i've placed a special page template comment at the top of the code).

NOTE: When you save this file in your theme's folder, make sure not to give it the name of a theme template file, so don't call it category.php or archive.php(any filename native to WordPress themes), instead use something unique, eg. customfields-template.php (or whatever you like).

 * Template Name: CustomField Table

<div id="container">
    <div id="content">

        <?php if( have_posts() ) : ?>

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

                <h2><?php the_title(); ?></h2>

                // Array of custom fields to get(so enter your applicable field keys here)
                $fields = array( 'field-one', 'field-two' );

                // Array to hold data
                $custom_fields = array();

                // Loop over keys and fetch post meta for each, store into new array
                foreach( $fields as $custom_field_key )
                    $custom_fields[$custom_field_key] = get_post_meta( $post->ID, $custom_field_key, true );

                // Filter out any empty / false values
                $custom_fields = array_filter( $custom_fields );

                // If we have custom field data 
                if( !empty( $custom_fields ) )
                    $c = 0;
                        <?php foreach( $custom_fields as $field_name => $field_value ) : $c++; ?>
                        <tr<?php echo ( $c % 2 == 0 ) ? ' class="alt"' : '' ; ?>>
                            <td><?php echo $field_name ?></td>
                            <td><?php echo $field_value ?></td>
                        <?php endforeach; ?>

                <div <?php post_class(); ?>><?php the_content(); ?></div>

            <?php endwhile; ?>

        <?php endif; ?>


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

Since you're looking for particular keys and expect singular values, the easiest and most suitable function i feel to use would be get_post_meta, this function can select multiple or single values for a single custom field key(and automatically excludes private meta keys that you'd otherwise get with the get_post_custom_ functions.

More on those functions can be found here.

Create a simple array of the custom field keys to select data from, then loop over them calling get_post_meta with each iteration. Gather that meta data and place it into a new array so it can later be easily filtered of empty/false results(rather then repeated if( $var ) or if(!empty()) calls).

Next check the new array has some data, output a table and loop over the new array to build the rows of data.

Only thing you need change is this line(these are my test field names)..

$fields = array( 'field-one', 'field-two' );

So for example, to get custom fields with the keys, name, gender, job and description..

$fields = array( 'name', 'gender', 'job', 'description' );


  • I took the liberty of adding alternating classes to the table rows
  • I assumed you'd want post title and content to(remove if not needed)
  • HTML markup is based on the TwentyTen theme, if your theme uses different markup you'll need to update the HTML as needed(or you can link me to one of your webpages and i'll refactor the sample code for you)
  • I wasn't sure how you wanted the data displayed in the table, so if you had a different idea about how to layout the data in the table, let me know and i'll refactor the code for you.

Let me know if you have any questions regarding the code. :)

share|improve this answer
Yes, I have a few: name, gender, job, description. I think they will only have one value. – Ambo100 Feb 17 '11 at 23:17
Have updated my answer for you... :) – t31os Feb 18 '11 at 0:07
Thankyou very much! I've modified the table design slightly. I'll be using this template to record citizens for a Lego City. You can see an example here. – Ambo100 Feb 18 '11 at 12:19
Happy to help.. :) – t31os Feb 18 '11 at 12:41

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