Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

metadata - wordpress featured posts

i'm trying to create a portfolio website using wordpress,

each post has view costum fields, one of which is called type - with the value of "featured" or "not-featured"

now when user clicks on the post title - they go the the single.php to see the entire post, here i would love to display all featured thumbnails

i tried this

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

      <?php  if(get_post_meta($post->ID, 'type', true) == "featured") {; ?>
  <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"> 
<img src="<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" />
</a></h2>
<?php  }; ?>
<div class="entry-content">

     </div><!– .entry-content –> 
      <?php endwhile; ?> 

(THIS CODE IS SIMILAR TO THE CODE I USE AT INDEX.PHP AND THERE IT DOES WORK, HERE AT SINGLE.PHP IT DOES NOT WORK)

but this does not display all the thumbnails (only the current posts' thumbnail (is it's a feature post))

this is my first attempt of trying to create a theme from blank so i'm not sure what the error could be

thanks for your help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code in your question only loops through the posts returned by the query made for the current view, in the case of a single post view that is one post. You want to perform a new query to retrieve all the posts that have the required meta value:

<?php
  query_posts(array("meta_key" => "type", "meta_value" => "featured"));
  if (have_posts()) : while (have_posts()) : the_post();
?>
  <!-- Display thumbnails -->
<?php endwhile; endif; ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...