0

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...

8
  • 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
    Commented Feb 17, 2011 at 20:03
  • @Rarst It's Arras 1.5.0.1
    – Ambo100
    Commented Feb 17, 2011 at 20:13
  • 2
    WordPress Answers is not RentACoderForFree. Read the Codex first and then come asking questions when you're stuck.
    – scribu
    Commented Feb 17, 2011 at 20:25
  • @scribu Fair enough, Where should I request help?
    – Ambo100
    Commented Feb 17, 2011 at 20:27
  • Try jobs.wordpress.net or codepoet.com
    – scribu
    Commented Feb 17, 2011 at 20:28

1 Answer 1

4

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).

<?php
/**
 * Template Name: CustomField Table
 */
get_header(); 
?>

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

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

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

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

                <?php
                // 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;
                ?>
                <table>
                    <thead>
                        <tr>
                            <th>Field</th>
                            <th>Value</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?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>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <th>Field</th>
                            <th>Value</th>
                        </tr>
                    </tfoot>
                </table>

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

            <?php endwhile; ?>

        <?php endif; ?>

    </div>
</div>

<?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.
http://codex.wordpress.org/Custom_Fields#Function_Reference

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' );

NOTES:

  • 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. :)

4
  • Yes, I have a few: name, gender, job, description. I think they will only have one value.
    – Ambo100
    Commented Feb 17, 2011 at 23:17
  • Have updated my answer for you... :)
    – t31os
    Commented Feb 18, 2011 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
    Commented Feb 18, 2011 at 12:19
  • Happy to help.. :)
    – t31os
    Commented Feb 18, 2011 at 12:41

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