2012-03-29 15 views
0

本当に助けてくれることを願っています。WordPressのホームページにカスタムポストタイプのメタデータを表示する

私はクライアント用のテーマを作成しています。私は「お客様の声」と呼ばれるカスタム投稿タイプを作成しましたが、今ではホームページの紹介文を表示したいのですが、メタデータの出力に問題があります情報。

私はfunctions.phpファイルで 'show_testimonials'という名前の関数を作成しました。私はindex.phpファイルでその呼び出しを行いました(私はそれらの構成要素にすべてを分けていません。例えばheader.php、 footer.phpなど)。この機能は、「功績」のカスタム投稿の投稿とメタ情報を表示するためのものですが、実際の投稿のタイプとは異なります。

正しいメタデータを '$ meta'変数に引き出していることがわかります。

以下は 'show_testimonials'機能のコードです。

function show_testimonials() { 
    $loop = new WP_Query(
     array(
     'post_type' => 'shv_testimonials', 
     'order_by' => 'ID', 
     ) 
    ); 

    if ($loop->have_posts()) { 
     $output = '<ul class="testimonial-list">'; 

     while($loop->have_posts()) { 
      $loop->the_post(); 
      $meta = get_post_meta(get_the_id(), ''); 
      print_r($meta); 

      $output .= ' 
       <li> 
        <a href="' . get_permalink() . '"> 
         ' . get_the_title() . ' | ' . 
         $meta['shv_testimonial_author'][0] . ' 
        </a> 
       </li> 
      '; 
     } 
    } 

    return $output; 
} 

以下は私のindex.phpファイルです。

<!DOCUMENT html> 
<!--[if IE 6]> 
<html id="ie6" <?php language_attributes(); ?>> 
<![endif]--> 
<!--[if IE 7]> 
<html id="ie7" <?php language_attributes(); ?>> 
<![endif]--> 
<!--[if IE 8]> 
<html id="ie8" <?php language_attributes(); ?>> 
<![endif]--> 
<!--[if !(IE 6) | !(IE 7) | !(IE) ]><!--> 
<html <?php language_attributes(); ?>> 
<!--<![endif]--> 
<head> 
    <meta charset="<?php bloginfo('charset');?>" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
    <title> 
     <?php 

      global $page, $paged; 

      wp_title('&laquo;', true, 'right'); 

      bloginfo('name'); 

     ?> 
    </title> 
    <link rel="profile" href="http://gmpg.org/xfn/11" /> 
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> 
    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> 
    <?php if (is_singular() && get_option('thread_comments')) wp_enqueue_script('comment-reply'); ?> 
    <?php wp_enqueue_script('jquery'); ?> 
    <?php wp_head(); ?> 
</head> 
<body <?php body_class(); ?>> 
    <div id="page"> 
     <header id="masthead" role="banner"> 
      <div id="header-strip"> 
       <nav id="secondary-navigation" role="navigation"> 
        <?php wp_nav_menu(array(
         'menu' => 'Secondary Navigation', 
         'container' => false 
        )); ?> 
       </nav> 
       <?php get_search_form(true); ?> 
      </div> 
      <div id="branding-nav"> 
       <?php 
        if (is_front_page()) { ?> 
         <div id="logo" role="banner"> 
          <img src="<?php echo bloginfo('template_directory') . '/images/logo.png'; ?>" alt="<?php bloginfo('name'); ?>" /> 
         </div> 
        <?php } else { ?> 
         <div id="logo" role="banner"> 
          <a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>"><img src="<?php echo bloginfo('template_directory') . '/images/logo.png'; ?>" alt="<?php bloginfo('name'); ?>" /></a> 
         </div> 
        <?php } 
       ?> 
       <nav id="primary-navigation" role="navigation"> 
        <?php wp_nav_menu(array(
         'menu' => 'Primary Navigation', 
         'container' => false 
        )); ?> 
       </nav> 
      </div> 
     </header> 
     <div id="content"> 
       <?php 
       if(is_front_page()) { ?> 
        <div id="feature"> 
         <section id="introduction"> 
          <?php 
           echo "<h1 class='title'>" . $options['shv_introduction_heading'] . '</h1>'; 
           echo "<p class='short-description'>" . $options['shv_introduction_short_description'] . '</p>'; 
          ?> 
          <nav id="call-to-action" role="navigation"> 
           <ul> 
            <li id="book-now"> 
             <a href="<?php echo get_bloginfo('url') . '/bookings/' ?>" title="Book Now">Book Now</a> 
            </li> 
            <li id="learn-more"> 
             <a href="<?php echo get_bloginfo('url') . '/what-we-do/' ?>" title="Learn More">Learn More</a> 
            </li> 
           </ul> 
          </nav> 
         </section> 
         <section id="gallery"> 

         </section> 
        </div> 
       <?php } else { ?> 
        <section id="entries"> 
         <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
           <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="main"> 
            <header> 
             <aside class="post-image"> 
              <?php 
              if (has_post_thumbnail()) { 
               the_post_thumbnail(); 
              } else { ?> 
               <a href="<?php the_permalink() ?>"><img src="<?php echo get_bloginfo('template_directory') . '/images/no-image.png'; ?>" title="<?php the_title(); ?>" /></a> 
              <?php }?> 
             </aside> 
             <h1 id="post-<?php the_ID(); ?>"> 
              <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> 
             </h2> 
             <time datetime="YYYY-MM-DD"></time> 
            </header> 
            <section class="post-content"> 
             <p><?php echo get_the_excerpt(); ?></p> 
             <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>" class="read_more ">Read More</a> 
            </section> 
            <section class="meta"> 
             <p><?php the_category(',') ?></p> 
             <p><?php the_tags(""); ?></p> 
            </section> 
           </article> 
         <?php endwhile; else: ?> 
           <div class="error"> 
            <h2>Oooops!</h2> 
            <p>Sorry something went wrong! No posts matched your criteria. Please try again.</p> 
           </div> 
         <?php endif; ?> 
         <?php posts_nav_link(' ⏼ ', __('« Newer Posts'), __('Older Posts »')); ?> 
         <aside role="sidebar"> 
          <h2>Some Widget in The Sidebar</h2> 
         </aside> 
        </section> 
       <?php } ?> 
     </div> 
     <footer role="foottext"> 
      <div id="footer-top"> 
       <section id="social"> 
        <div id="twitter"> 
         <?php show_tweets(); ?> 
        </div> 
        <div id="share"> 
         <h2>Share</h2> 
         <div id="icons"> 
          <a href="http://twitter.com/home?status=Shiverschool Theatre in Education+http://www.shiverschool.co.uk/" title="Tweet About Us"> 
           <img src="<?php echo get_bloginfo('template_directory') . '/images/twitter-share-icon.png'; ?>" alt="Tweet About Us" /> 
          </a> 
          <a href="http://www.stumbleupon.com/submit?url={http://www.shiverschool.co.uk}&amp;title={Shiverschool Theatre in Education}" title="Recommend us on StumbleUpon">         
           <img src="<?php echo get_bloginfo('template_directory') . '/images/stumbleupon-share-icon.png'; ?>" alt="Recommend us on StumbleUpon" /> 
          </a> 
          <a href="http://digg.com/submit?url=http://www.shiverschool.co.uk/" title="Digg Us"> 
           <img src="<?php echo get_bloginfo('template_directory') . '/images/digg-share-icon.png'; ?>" alt="Digg Us" /> 
          </a> 
          <a href="http://www.facebook.com/sharer.php?u=http://www.shiverschool.co.uk&amp;t=Shiverschool Theatre in Education" title="Recommend us on Facebook"> 
           <img src="<?php echo get_bloginfo('template_directory') . '/images/facebook-share-icon.png'; ?>" alt="Recommend us on Facebook" /> 
          </a>  
          <a href="https://plusone.google.com/_/+1/confirm?hl=en&url=http://www.shiverschool.co.uk&title=Shiverschool Theatre in Education" title="Recommend us on Google+"> 
           <img src="<?php echo get_bloginfo('template_directory') . '/images/google-share-icon.png'; ?>" alt="Recommend us on Google+" /> 
          </a> 
         </div> 
        </div> 
       </section> 
       <section id="testimonials"> 
        <div id="testimonial"> 
         <?php show_testimonials(); ?> 
        </div> 
        <div id="mascot"> 
         <img src="<?php echo bloginfo('template_directory') . '/images/mascot.png' ?>" alt="Shiverschool Mascot" /> 
        </div> 
       </section> 
      </div> 
      <?php wp_footer(); ?> 
     </footer> 
    </div> 
</body> 
</html> 

ここには完全な 'functions.php'ファイルがあります。

<?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' => 'Primary Navigation', 
      'secondary' => 'Secondary Navigation' 
     )); 
    } 

/* 
* Register custom post types 
*/ 

//Testimonials Post Type 
class SHV_Testimonials_Post_Type { 

    //Create construct function 
    public function __construct() { 
     $this->register_post_type(); 
     $this->metaboxes(); 
    } 

    //Register post type 
    public function register_post_type() { 
     $args = array(
      'labels' => array(
       'name' => 'Testimonials', 
       'singular_' => 'Testimonial', 
       'add_new' => 'Add New Testimonial', 
       'all_items' => 'All Testimonials', 
       'add_new_item' => 'Add New Testimonial', 
       'edit_item' => 'Edit Testimonial', 
       'new_item' => 'New Testimonial', 
       'view_item' => 'View Testimonial', 
       'search_items' => 'Search Testimonials', 
       'not_found' => 'No Testimonials Found', 
       'not_found_in_trash' => 'No Testimonials Found in Trash' 
      ), 
      'query_var' => 'testimonials', 
      'rewrite' => array(
       'slug' => 'testimonials', 
      ), 
      'public' => true, 
      'supports' => 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->ID, 'shv_testimonial', true); ?> 
      <p> 
       <label for="shv_testimonial">Please enter your testimonial</label> 
       <textarea class="large-text" name="shv_testimonial" id="shv_testimonial" cols="50" rows="5"><?php echo esc_attr($testimonial); ?></textarea> 
      </p> 
     <?php } 

     function testimonial_author($post) { 
      $author = get_post_meta($post->ID, 'shv_testimonial_author', true); ?> 
      <p> 
       <label for="shv_testimonial_author">Please enter a testimonial author name and school, e.g. John Doe, St Ambrose Barlow</label> 
       <input type="text" class="widefat" name="shv_testimonial_author" id="shv_testimonial_author" value="<?php echo esc_attr($author); ?>" /> 
      </p> 
     <?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() { ?> 
    <img src="<?php echo get_bloginfo('template_directory') . '/images/twitter-bird-icon.png'; ?>" alt="Tweets" /> 
     <?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->get_item_quantity(1); 

       // Build an array of all the items, starting with element 0 (first element). 
       $rss_items = $rss->get_items(0, $maxitems); 
      endif; 
     ?> 
     <ul> 
      <?php if ($maxitems == 0) echo '<li>No items.</li>'; 
      else 
      // Loop through each feed item and display each item as a hyperlink. 
      foreach ($rss_items as $item) : ?> 
       <li> 
        <a href='<?php echo $item->get_permalink(); ?>'> 
         <?php echo $item->get_title(); ?> 
        </a> 
       </li> 
      <?php endforeach; ?> 
     </ul> 
<?php } 

function show_testimonials() { 
    $loop = new WP_Query(
     array(
     'post_type' => 'shv_testimonials', 
     'order_by' => 'ID', 
     ) 
    ); 

    if ($loop->have_posts()) { 
     $output = '<ul class="testimonial-list">'; 

     while($loop->have_posts()) { 
      $loop->the_post(); 
      $meta = get_post_meta(get_the_id(), ''); 
      print_r($meta); 

      $output .= ' 
       <li> 
        <a href="' . get_permalink() . '"> 
         ' . get_the_title() . ' | ' . 
         $meta['shv_testimonial_author'][0] . ' 
        </a> 
       </li> 
      '; 
     } 
    } 

    return $output; 
} 
?> 

は、ここでは、私が使用して、正しいメタデータを出力していていることがわかります開発サイトへのリンクの「ますprint_r($メタを);」。 http://dev.garethdaine.com/shiverschool

誰かが正しい方向に向けることを願っています。あらかじめ乾杯。 show_testimonialsの出力は、単にどこにも印刷されていない

答えて

1

、あなたのshow_testimonials機能に代わりreturn $output;のテンプレートでecho show_testimonials()またはecho $output;を試してみてください。

+1

ありがとうございました。私は 'index.php'のコードでそれを持っていましたが、エコーを忘れました。シンプルなタイプミス、スポッティングのおかげで。 :) –

関連する問題