title: Displaying Custom Post Type Meta Data on WordPress Home Page
body: <p>I really hope you can help out here.</p>
  
  <p>I'm creating a theme for a client and I have created a custom post type called 'Testimonials' and now I would like to show the testimonials on the home page but I'm having trouble outputting the data for the meta information.</p>
  
  <p>I have created a function in my functions.php file called 'show_testimonials' and I'm calling that in my index.php file (I haven't separated everything in to their constituant parts yet, e.g header.php, footer.php etc). This function is meant to show the posts and meta information from the 'Testimonials' custom post type but it doesn't.</p>
  
  <p>I can see that I am pulling the correct meta data in to my '$meta' variable.</p>
  
  <p>Below is the code for the 'show_testimonials' function.</p>
  
  <pre><code>function show_testimonials() {
      $loop = new WP_Query(
          array(
          'post_type' =&gt; 'shv_testimonials',
          'order_by' =&gt; 'ID',
          )
      );
  
      if ($loop-&gt;have_posts()) {
          $output = '&lt;ul class="testimonial-list"&gt;';
  
          while($loop-&gt;have_posts()) {
              $loop-&gt;the_post();
              $meta = get_post_meta(get_the_id(), '');
              print_r($meta);
  
              $output .= '
                  &lt;li&gt;
                      &lt;a href="' . get_permalink() . '"&gt;
                          ' . get_the_title() . ' | ' .
                          $meta['shv_testimonial_author'][0] . '
                      &lt;/a&gt;
                  &lt;/li&gt;
              ';
          }
      }
  
      return $output;
  }
  </code></pre>
  
  <p>Below is my index.php file.</p>
  
  <pre><code>&lt;!DOCUMENT html&gt;
  &lt;!--[if IE 6]&gt;
  &lt;html id="ie6" &lt;?php language_attributes(); ?&gt;&gt;
  &lt;![endif]--&gt;
  &lt;!--[if IE 7]&gt;
  &lt;html id="ie7" &lt;?php language_attributes(); ?&gt;&gt;
  &lt;![endif]--&gt;
  &lt;!--[if IE 8]&gt;
  &lt;html id="ie8" &lt;?php language_attributes(); ?&gt;&gt;
  &lt;![endif]--&gt;
  &lt;!--[if !(IE 6) | !(IE 7) | !(IE) ]&gt;&lt;!--&gt;
  &lt;html &lt;?php language_attributes(); ?&gt;&gt;
  &lt;!--&lt;![endif]--&gt;
  &lt;head&gt;
      &lt;meta charset="&lt;?php bloginfo('charset');?&gt;" /&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"&gt;
      &lt;title&gt;
          &lt;?php 
  
              global $page, $paged;
  
              wp_title('&amp;laquo;', true, 'right');
  
              bloginfo('name');
  
          ?&gt;
      &lt;/title&gt;
      &lt;link rel="profile" href="http://gmpg.org/xfn/11" /&gt;
      &lt;link rel="stylesheet" href="&lt;?php bloginfo('stylesheet_url'); ?&gt;" type="text/css" media="screen" /&gt;
      &lt;link rel="pingback" href="&lt;?php bloginfo('pingback_url'); ?&gt;" /&gt;
      &lt;?php if ( is_singular() &amp;&amp; get_option('thread_comments')) wp_enqueue_script('comment-reply'); ?&gt;
      &lt;?php wp_enqueue_script('jquery'); ?&gt;
      &lt;?php wp_head(); ?&gt;
  &lt;/head&gt;
  &lt;body &lt;?php body_class(); ?&gt;&gt;
      &lt;div id="page"&gt;
          &lt;header id="masthead" role="banner"&gt;
              &lt;div id="header-strip"&gt;
                  &lt;nav id="secondary-navigation" role="navigation"&gt;
                      &lt;?php wp_nav_menu(array(
                          'menu' =&gt; 'Secondary Navigation',
                          'container' =&gt; false
                      )); ?&gt;
                  &lt;/nav&gt;
                  &lt;?php get_search_form(true); ?&gt; 
              &lt;/div&gt;
              &lt;div id="branding-nav"&gt;
                  &lt;?php
                      if (is_front_page()) { ?&gt;
                          &lt;div id="logo" role="banner"&gt;
                              &lt;img src="&lt;?php echo bloginfo('template_directory') . '/images/logo.png'; ?&gt;" alt="&lt;?php bloginfo('name'); ?&gt;" /&gt; 
                          &lt;/div&gt;
                      &lt;?php } else { ?&gt;
                          &lt;div id="logo" role="banner"&gt;
                              &lt;a href="&lt;?php bloginfo('url'); ?&gt;" title="&lt;?php bloginfo('name'); ?&gt;"&gt;&lt;img src="&lt;?php echo bloginfo('template_directory') . '/images/logo.png'; ?&gt;" alt="&lt;?php bloginfo('name'); ?&gt;" /&gt;&lt;/a&gt; 
                          &lt;/div&gt;
                      &lt;?php }
                  ?&gt;
                  &lt;nav id="primary-navigation" role="navigation"&gt;
                      &lt;?php wp_nav_menu(array(
                          'menu' =&gt; 'Primary Navigation',
                          'container' =&gt; false
                      )); ?&gt;
                  &lt;/nav&gt;
              &lt;/div&gt;
          &lt;/header&gt;
          &lt;div id="content"&gt;
                  &lt;?php 
                  if(is_front_page()) { ?&gt;
                      &lt;div id="feature"&gt;
                          &lt;section id="introduction"&gt;
                              &lt;?php
                                  echo "&lt;h1 class='title'&gt;" . $options['shv_introduction_heading'] . '&lt;/h1&gt;';
                                  echo "&lt;p class='short-description'&gt;" . $options['shv_introduction_short_description'] . '&lt;/p&gt;';
                              ?&gt;
                              &lt;nav id="call-to-action" role="navigation"&gt;
                                  &lt;ul&gt;
                                      &lt;li id="book-now"&gt;
                                          &lt;a href="&lt;?php echo get_bloginfo('url') . '/bookings/' ?&gt;" title="Book Now"&gt;Book Now&lt;/a&gt;
                                      &lt;/li&gt;
                                      &lt;li id="learn-more"&gt;
                                          &lt;a href="&lt;?php echo get_bloginfo('url') . '/what-we-do/' ?&gt;" title="Learn More"&gt;Learn More&lt;/a&gt;
                                      &lt;/li&gt;
                                  &lt;/ul&gt;
                              &lt;/nav&gt;
                          &lt;/section&gt;
                          &lt;section id="gallery"&gt;
  
                          &lt;/section&gt;
                      &lt;/div&gt;
                  &lt;?php } else { ?&gt;
                      &lt;section id="entries"&gt;
                          &lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
                                  &lt;article id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class(); ?&gt; role="main"&gt;
                                      &lt;header&gt;
                                          &lt;aside class="post-image"&gt;  
                                              &lt;?php  
                                              if (has_post_thumbnail()) {  
                                                  the_post_thumbnail();  
                                              } else { ?&gt;  
                                                  &lt;a href="&lt;?php the_permalink() ?&gt;"&gt;&lt;img src="&lt;?php echo get_bloginfo('template_directory') . '/images/no-image.png'; ?&gt;" title="&lt;?php the_title(); ?&gt;" /&gt;&lt;/a&gt;  
                                              &lt;?php }?&gt;  
                                          &lt;/aside&gt; 
                                          &lt;h1 id="post-&lt;?php the_ID(); ?&gt;"&gt;
                                              &lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
                                          &lt;/h2&gt;
                                          &lt;time datetime="YYYY-MM-DD"&gt;&lt;/time&gt;
                                      &lt;/header&gt;
                                      &lt;section class="post-content"&gt;
                                          &lt;p&gt;&lt;?php echo get_the_excerpt(); ?&gt;&lt;/p&gt;
                                          &lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="&lt;?php the_title(); ?&gt;" class="read_more "&gt;Read More&lt;/a&gt;
                                      &lt;/section&gt;
                                      &lt;section class="meta"&gt;
                                          &lt;p&gt;&lt;?php the_category(',') ?&gt;&lt;/p&gt;
                                          &lt;p&gt;&lt;?php the_tags(""); ?&gt;&lt;/p&gt;
                                      &lt;/section&gt;
                                  &lt;/article&gt;
                          &lt;?php endwhile; else: ?&gt;
                                  &lt;div class="error"&gt;
                                      &lt;h2&gt;Oooops!&lt;/h2&gt;
                                      &lt;p&gt;Sorry something went wrong! No posts matched your criteria. Please try again.&lt;/p&gt;
                                  &lt;/div&gt;
                          &lt;?php endif; ?&gt;
                          &lt;?php posts_nav_link(' ⏼ ', __('« Newer Posts'), __('Older Posts »')); ?&gt; 
                          &lt;aside role="sidebar"&gt;  
                              &lt;h2&gt;Some Widget in The Sidebar&lt;/h2&gt;  
                          &lt;/aside&gt;
                      &lt;/section&gt;
                  &lt;?php } ?&gt;
          &lt;/div&gt;
          &lt;footer role="foottext"&gt;  
              &lt;div id="footer-top"&gt;
                  &lt;section id="social"&gt;
                      &lt;div id="twitter"&gt;
                          &lt;?php show_tweets(); ?&gt;
                      &lt;/div&gt;
                      &lt;div id="share"&gt;
                          &lt;h2&gt;Share&lt;/h2&gt;
                          &lt;div id="icons"&gt;
                              &lt;a href="http://twitter.com/home?status=Shiverschool Theatre in Education+http://www.shiverschool.co.uk/" title="Tweet About Us"&gt;
                                  &lt;img src="&lt;?php echo get_bloginfo('template_directory') . '/images/twitter-share-icon.png'; ?&gt;" alt="Tweet About Us" /&gt;
                              &lt;/a&gt;
                              &lt;a href="http://www.stumbleupon.com/submit?url={http://www.shiverschool.co.uk}&amp;amp;title={Shiverschool Theatre in Education}" title="Recommend us on StumbleUpon"&gt;                                  
                                  &lt;img src="&lt;?php echo get_bloginfo('template_directory') . '/images/stumbleupon-share-icon.png'; ?&gt;" alt="Recommend us on StumbleUpon" /&gt;
                              &lt;/a&gt;
                              &lt;a href="http://digg.com/submit?url=http://www.shiverschool.co.uk/" title="Digg Us"&gt; 
                                  &lt;img src="&lt;?php echo get_bloginfo('template_directory') . '/images/digg-share-icon.png'; ?&gt;" alt="Digg Us" /&gt;
                              &lt;/a&gt;
                              &lt;a href="http://www.facebook.com/sharer.php?u=http://www.shiverschool.co.uk&amp;amp;t=Shiverschool Theatre in Education" title="Recommend us on Facebook"&gt; 
                                  &lt;img src="&lt;?php echo get_bloginfo('template_directory') . '/images/facebook-share-icon.png'; ?&gt;" alt="Recommend us on Facebook" /&gt;
                              &lt;/a&gt;    
                              &lt;a href="https://plusone.google.com/_/+1/confirm?hl=en&amp;url=http://www.shiverschool.co.uk&amp;title=Shiverschool Theatre in Education" title="Recommend us on Google+"&gt; 
                                  &lt;img src="&lt;?php echo get_bloginfo('template_directory') . '/images/google-share-icon.png'; ?&gt;" alt="Recommend us on Google+" /&gt;
                              &lt;/a&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/section&gt;
                  &lt;section id="testimonials"&gt;
                      &lt;div id="testimonial"&gt;
                          &lt;?php show_testimonials(); ?&gt;
                      &lt;/div&gt;
                      &lt;div id="mascot"&gt;
                          &lt;img src="&lt;?php echo bloginfo('template_directory') . '/images/mascot.png' ?&gt;" alt="Shiverschool Mascot" /&gt;
                      &lt;/div&gt;
                  &lt;/section&gt;
              &lt;/div&gt;
              &lt;?php wp_footer(); ?&gt;
          &lt;/footer&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is the full 'functions.php' file.</p>
  
  <pre><code>&lt;?php
  // Add JavaScript files
  function add_scripts() {
      wp_enqueue_script('modernizr_script', get_template_directory_uri() . '/js/modernizr.js', __FILE__);
      wp_enqueue_script('global_functions_script', get_template_directory_uri() . '/js/global-functions.js', __FILE__);
  }
  add_action('wp_enqueue_scripts', 'add_scripts');
  
  // Setup and load the theme options page and related code
  require(get_template_directory() . '/inc/theme-options.php');
  
  //Get theme options
  $options = get_option('shv_theme_options');
  
  // Add support for custom backgrounds
  add_custom_background();
  
  // Add support for post thumbnails and admin bar
  add_theme_support('post-thumbnails');
  add_theme_support('admin-bar');
  
  // This theme uses wp_nav_menu() in two locations
      if (function_exists('register_nav_menus')) {
          register_nav_menus(array(
              'primary' =&gt; 'Primary Navigation',
              'secondary' =&gt; 'Secondary Navigation'
          ));
      }
  
  /*
   * Register custom post types
   */
  
  //Testimonials Post Type
  class SHV_Testimonials_Post_Type {
  
      //Create construct function
      public function __construct() {
          $this-&gt;register_post_type();
          $this-&gt;metaboxes();
      }
  
      //Register post type
      public function register_post_type() {
          $args = array(
              'labels' =&gt; array(
                  'name' =&gt; 'Testimonials',
                  'singular_' =&gt; 'Testimonial',
                  'add_new' =&gt; 'Add New Testimonial',
                  'all_items' =&gt; 'All Testimonials',
                  'add_new_item' =&gt; 'Add New Testimonial',
                  'edit_item' =&gt; 'Edit Testimonial',
                  'new_item' =&gt; 'New Testimonial',
                  'view_item' =&gt; 'View Testimonial',
                  'search_items' =&gt; 'Search Testimonials',
                  'not_found' =&gt; 'No Testimonials Found',
                  'not_found_in_trash' =&gt; 'No Testimonials Found in Trash'
              ),
              'query_var' =&gt; 'testimonials',
              'rewrite' =&gt; array(
                  'slug' =&gt; 'testimonials',
              ),
              'public' =&gt; true,
              'supports' =&gt; array(
                  'title'
              )
          );
          register_post_type('shv_testimonials', $args);
      }
  
      //Build metaboxes
      public function metaboxes() {
  
          //Add new meta boxes testimonial and testimonial author
          add_action('add_meta_boxes', 'add_testimonial_meta_box');
          add_action('add_meta_boxes', 'add_testimonial_author_meta_box');
  
          //Run add_meta_box functions
          function add_testimonial_meta_box() {
              add_meta_box('shv_testimonial', 'Testimonial', 'testimonial', 'shv_testimonials');
          }
          function add_testimonial_author_meta_box() {
              add_meta_box('shv_testimonial_author', 'Testimonial Author', 'testimonial_author', 'shv_testimonials');
          }
  
          //Create form elements and pull through any data associated with each meta box
          function testimonial($post) {
              $testimonial = get_post_meta($post-&gt;ID, 'shv_testimonial', true); ?&gt;
              &lt;p&gt;
                  &lt;label for="shv_testimonial"&gt;Please enter your testimonial&lt;/label&gt;
                  &lt;textarea class="large-text" name="shv_testimonial" id="shv_testimonial" cols="50" rows="5"&gt;&lt;?php echo esc_attr($testimonial); ?&gt;&lt;/textarea&gt;
              &lt;/p&gt;
          &lt;?php }
  
          function testimonial_author($post) {
              $author = get_post_meta($post-&gt;ID, 'shv_testimonial_author', true); ?&gt;
              &lt;p&gt;
                  &lt;label for="shv_testimonial_author"&gt;Please enter a testimonial author name and school, e.g. John Doe, St Ambrose Barlow&lt;/label&gt;
                  &lt;input type="text" class="widefat" name="shv_testimonial_author" id="shv_testimonial_author" value="&lt;?php echo esc_attr($author); ?&gt;" /&gt;
              &lt;/p&gt;
          &lt;?php }
  
          //Save meta box input
          add_action('save_post', 'save_meta_data');
  
          function save_meta_data($id) {
  
              if (isset($_POST['shv_testimonial'])) {
                  update_post_meta(
                      $id,
                      'shv_testimonial',
                      strip_tags($_POST['shv_testimonial'])
                  );
              }
  
              if (isset($_POST['shv_testimonial_author'])) {
                  update_post_meta(
                      $id,
                      'shv_testimonial_author',
                      strip_tags($_POST['shv_testimonial_author'])
                  );
              }
          }
      }
  }
  
  add_action('init', 'register_testimonials');
  
  function register_testimonials() {
      new SHV_Testimonials_Post_Type();
  }
  
  function show_tweets() { ?&gt;
      &lt;img src="&lt;?php echo get_bloginfo('template_directory') . '/images/twitter-bird-icon.png'; ?&gt;" alt="Tweets" /&gt;
          &lt;?php
              //Get theme options
              $options = get_option('shv_theme_options');
              include_once(ABSPATH . WPINC . '/feed.php');
              $username = $options['shv_twitter_username'];
              $rss = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=' . $username);
  
              if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 
                  // Figure out how many total items there are, but limit it to 5. 
                  $maxitems = $rss-&gt;get_item_quantity(1); 
  
                  // Build an array of all the items, starting with element 0 (first element).
                  $rss_items = $rss-&gt;get_items(0, $maxitems); 
              endif;
          ?&gt;
          &lt;ul&gt;
              &lt;?php if ($maxitems == 0) echo '&lt;li&gt;No items.&lt;/li&gt;';
              else
              // Loop through each feed item and display each item as a hyperlink.
              foreach ( $rss_items as $item ) : ?&gt;
                  &lt;li&gt;
                      &lt;a href='&lt;?php echo $item-&gt;get_permalink(); ?&gt;'&gt;
                          &lt;?php echo $item-&gt;get_title(); ?&gt;
                      &lt;/a&gt;
                  &lt;/li&gt;
              &lt;?php endforeach; ?&gt;
          &lt;/ul&gt;
  &lt;?php }
  
  function show_testimonials() {
      $loop = new WP_Query(
          array(
          'post_type' =&gt; 'shv_testimonials',
          'order_by' =&gt; 'ID',
          )
      );
  
      if ($loop-&gt;have_posts()) {
          $output = '&lt;ul class="testimonial-list"&gt;';
  
          while($loop-&gt;have_posts()) {
              $loop-&gt;the_post();
              $meta = get_post_meta(get_the_id(), '');
              print_r($meta);
  
              $output .= '
                  &lt;li&gt;
                      &lt;a href="' . get_permalink() . '"&gt;
                          ' . get_the_title() . ' | ' .
                          $meta['shv_testimonial_author'][0] . '
                      &lt;/a&gt;
                  &lt;/li&gt;
              ';
          }
      }
  
      return $output;
  }
  ?&gt;
  </code></pre>
  
  <p>Here's a link to the development site where you can see that I'm outputting the correct meta data using 'print_r($meta);'. <a href="http://dev.garethdaine.com/shiverschool" rel="nofollow">http://dev.garethdaine.com/shiverschool</a></p>
  
  <p>I hope someone can point me in the right direction. Cheers in advance.</p>