循环以从名为“Portfolio”的3个最新的自定义帖子类型中获取3个Post_Thumb实例

时间:2012-04-10 作者:mattc

我试图在我的主页模板上显示自定义帖子类型“公文包”中的3个post\\u缩略图。因此,一个来自post“A”,一个来自post“B”,一个来自post“C”。post A中的图像将设置为post\\u缩略图(“大”),而B&;C需要设置为post\\u缩略图(“缩略图”)。

我已经设置了一个自定义的WP\\u查询循环,正如预期的那样,它正在循环并获取最近三篇“公文包”文章中每一篇文章缩略图的三个实例。我只是不知道如何修改循环以只显示三幅图像,而不是三组三幅图像。

非常感谢您的帮助。

这是我的代码:

    <?php get_header(); ?>


    <div id="content" class="clearfix row">     
<article id="post-<?php the_ID(); ?>" <?php post_class(\'clearfix\'); ?>       role="article">


    <section id="work" class="row">      

        <div class="span3">
          <h3>Recent Work</h3>
          <p>Intro Lorem ipsum</p>
        </div>
          <?php $first_query = new WP_Query(\'post_type=portfolio\');   while($first_query->have_posts()) : $first_query->the_post(); ?>
        <div class="span6">
            <a href="single-project.php">
            <?php the_post_thumbnail(\'medium\'); ?> <!--Should be from custom post \'A\' -->
            </a>
        </div>

        <div class="span3">
            <a href="single-project.php" >
             <?php the_post_thumbnail(\'thumbnail\'); ?><!--Should be from custom post \'B\' -->
            </a>

          <a href="single-project.php">
           <?php the_post_thumbnail(\'thumbnail\'); ?><!--Should be from custom post \'C\' -->
          </a>  
        </div>

3 个回复
SO网友:shuvroMithun

尝试以下操作:

<?php get_header(); ?>
<div id="content" class="clearfix row">     
<article id="post-<?php the_ID(); ?>" <?php post_class(\'clearfix\'); ?> role="article">
    <section id="work" class="row">      
        <div class="span3">
            <h3>Recent Work</h3>
            <p>Intro Lorem ipsum</p>
        </div>          
        <?php $args = array(
            \'post_type\'      => \'portfolio\',
            \'post_status\'    => \'publish\',
            \'posts_per_page\' => 3
        );
        $query = new WP_Query( $args );
        $porfolio_post_ids = array();
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                global $post;
                $query->the_post();
                array_push( $porfolio_post_ids, $post->ID );
            }
        } ?>
        <div class="span6">
            <a href="<?php echo get_permalink( $porfolio_post_ids[0] ); ?>">
                <?php echo get_the_post_thumbnail(  
                    $porfolio_post_ids[0] , \'medium\' 
                ); ?> <!--Should be from custom post \'A\' -->
            </a>
        </div>   
        <div class="span3">
            <a href="<?php echo get_permalink( $porfolio_post_ids[1] ); ?>" >
                <?php echo get_the_post_thumbnail(  
                    $porfolio_post_ids[1] , \'thumbnail\' 
                ); ?><!--Should be from custom post \'B\' -->      
            </a>    
            <a href="<?php echo get_permalink( $porfolio_post_ids[2] ); ?>">
                <?php echo get_the_post_thumbnail( 
                    $porfolio_post_ids[2] , \'thumbnail\' 
                ); ?><!--Should be from custom post \'C\' -->
            </a>  
        </div>
不要忘记设置缩略图functions.php 正如David在回答中提到的。

我在你的代码中还注意到一件事:你说你的帖子类型是“公文包”,在你使用的永久链接中single-project.php, 但不是吗single-portfolio.php?

SO网友:Ben Huddleston

mattc,

您的问题是,所有元素都在循环中。您需要设置计数器,然后仅在需要时应用所需的项目。我只包含以下相关代码:

<?php 
 // set a variable to start a count
 $i = 0;

 $first_query = new WP_Query(\'post_type=portfolio\');   
 // the loop starts
 while($first_query->have_posts()) : $first_query->the_post(); 

 // add 1 to our variable on each pass of the loop
 $i++;

 // do the big image first
 if ($i == 1) {
 ?>
   <div class="span6">
        <a href="single-project.php">
        <?php the_post_thumbnail(\'medium\'); ?> <!--Should be from custom post \'A\' -->
        </a>
    </div>
 <?php } else { // the rest of the images ?>
 <?php if ($i == 2) {  // need to open our wrapping div here ?>
    <div class="span3">
 <?php } ?>
        <a href="single-project.php" >
         <?php the_post_thumbnail(\'thumbnail\'); ?><!--Should be from custom post \'B\' and \'C\' -->
        </a>
  <?php if ($i == 3) { // close out the wrapping div?>
    </div>
  <?php } ?>
  <?php endwhile; ?>

SO网友:David

我很好地使用了这个实现--您可以看到它是如何抓住它的:

$thumbnails = get_posts(array(\'numberposts\'=>1,\'orderby\'=>\'ASC\',\'meta_key\' => \'_thumbnail_id\'));

然后,您可以转到“numberposts”=>3并构建缩略图结构:

    <?php if ( has_post_thumbnail() ) {
      global $post;
      $thumbnails = get_posts(array(\'numberposts\'=>3,\'orderby\'=>\'ASC\',\'meta_key\' => \'_thumbnail_id\'));
      foreach ($thumbnails as $thumbnail) {
      echo \'<a href="\' . get_permalink( $post->ID ) . \'" title="\' . esc_attr( $post->post_title ) . \'">\';
      echo get_the_post_thumbnail($thumbnail->ID, \'small-thumb\', array( \'alt\' => esc_attr( $post->post_title ), \'title\' => esc_attr( $post->post_title ) ));
      echo \'</a>\';
      }        
     ?>   
已编辑:不要忘记设置缩略图大小:

echo get_the_post_thumbnail($thumbnail->ID, \'small-thumb\', array( \'alt\' => esc_attr( $post->post_title ), \'title\' => esc_attr( $post->post_title ) ));

将“小拇指”更改为已设置的任何大小,如果需要设置一些大小,可以将其放入函数中。php:

 //Add support, if you need it.
 if ( function_exists( \'add_theme_support\' ) ) {
   add_theme_support( \'post-thumbnails\' );
    set_post_thumbnail_size( 960, 276, true ); // default Post Thumbnail dimensions   
  }

 //Set them sizes, yo
 if ( function_exists( \'add_image_size\' ) ) { 
   add_image_size( \'large-thumb\', 960, 276, true ); //(cropped)
   add_image_size( \'medium-thumb\', 605, 174 ); // 605px wide, 174 high, not cropped
   add_image_size( \'small-thumb\', 288, 83, true ); //(cropped) 
   add_image_size( \'small-square\', 100, 100, true ); //(cropped) 
 }
干杯,

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post