wordpress featured posts

左心房为你撑大大i 提交于 2020-02-06 06:53:07

问题


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


回答1:


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; ?>


来源:https://stackoverflow.com/questions/3593112/wordpress-featured-posts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!