Custom Page Template [closed]

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

Leave a Comment