来自多个类别的帖子,但在第一个帖子中只有图片,其余的帖子只有帖子标题

时间:2012-10-17 作者:user22591

我很难只在每个类别的第一篇文章中设置特色图像,而其余的只显示文章标题。到目前为止,我一直设法用缩略图显示每个类别中的最新帖子,但我找不到方法,只能用缩略图显示第一篇帖子,其余的都是标题。有解决方案吗?这是我的代码:

                <?php
                    //get terms (category ids 11,2,33,34), then display one post in each term
                    $taxonomy = \'category\';//  e.g. post_tag, category
                    $param_type = \'category__in\'; //  e.g. tag__in, category__in
                    $term_args=array(
                      \'include\' => \'4,5,6,7\',
                      \'orderby\' => \'name\',
                      \'order\' => \'ASC\'
                    );
                    $terms = get_terms($taxonomy,$term_args);
                    if ($terms) {
                      foreach( $terms as $term ) {
                        $args=array(
                          "$param_type" => array($term->term_id),
                          \'post_type\' => \'post\',
                          \'post_status\' => \'publish\',
                          \'posts_per_page\' => 1,
                          \'caller_get_posts\'=> 1
                          );
                        $my_query = null;
                        $my_query = new WP_Query($args);
                        if( $my_query->have_posts() ) {
                          //Panikos Remove   echo \'Latest Post in \'.$taxonomy .\' \'.$term->name;
                          while ($my_query->have_posts()) : $my_query->the_post(); ?>
                            <div class="post_home_main">


                            <!--  Add timthumb to post featured image -->
                            <?php
                            $image_id = get_post_thumbnail_id();
                            $image_url = wp_get_attachment_url($image_id);
                            if ($image_url) {
                            ?>
                            <img src="<?php bloginfo(\'template_directory\'); ?>/scripts/timthumb.php?src=<?php echo $image_url; ?>&w=196&h=115&q=1000" alt="<?php the_title();?>" />
                             <!--  // Add timthumb to post featured image -->
                            <?php } else { ?>
                            <img src="<?php echo bloginfo(\'template_directory\') . \'/images/no-image.jpg\'; ?>" alt="No image available" class="panikosule" />
                            <?php } ?>


                            <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                            <?php the_excerpt(); ?> 
                            </div>
                           <?php
                          endwhile;
                        }
                      }
                    }
                    wp_reset_query();  // Restore global post data stomped by the_post().
                    ?>





    <span class="arrow"></span> </div>

3 个回复
SO网友:kaiser

一般注意事项:你不必使用(糟糕的)TimThumb工具。还有很多更好的东西,比如my(免费提供,开源)»Dynamic image resize« plugin.
  • 2nd(还有afaik),你可以用船上的东西来做这件事,比如get_image_tag() 在你的情况下
  • 3rd:不要将那么多变量放入全局名称空间。如果您真的认为无法解决此问题:请在它们前面加上一个唯一的字符串,如wpse69602_.\'include\' => \'4,5,6,7\'. 提示:大多数函数采用数组,而不是特殊的字符分隔字符串
  • 您清理的代码

    我用来识别当前循环帖子编号的kool不是$flag. 这是$wp_query 对象:Thecurrent_post, 这是WordPress已经设置的标志。

    <div class="container">
    <?php
    $terms = get_terms(
         \'category\'
        ,array(
             \'include\' => array( \'4,5,6,7\' )
            ,\'orderby\' => \'name\'
            ,\'order\'   => \'ASC\'
        )
    );
    if ( ! empty( $terms ) )
    {
        foreach( $terms as $term ) 
        {
            $my_query = new WP_Query( array(
                 \'category__in\'     => array( $term->term_id )
                ,\'post_type\'        => \'post\'
                ,\'post_status\'      => \'publish\'
                ,\'posts_per_page\'   => 1
                ,\'caller_get_posts\' => 1
            ) );
            if ( $my_query->have_posts() )
            {
                while ( $my_query->have_posts() )
                {
                    $my_query->the_post();
                    ?>
    
                    <div class="post_home_main">
    
                    <!-- Show the posts featured image -->
                    <?php
                    if ( 1 <= $my_query->current_post )
                    {
                        if ( $image_url = wp_get_attachment_url( get_post_thumbnail_id() ) )
                        {
                            echo get_image_tag(
                                 get_post_thumbnail_id()
                                ,__( \'Featured Image\', \'your_textdomain\' )
                                ,get_the_title()
                                ,\'none\'
                                 // Check your image sizes and adjust this one
                                ,\'thumb\'
                            );
                        }
                        else
                        {
                            ?>
                            <img src="<?php echo get_template_directory().\'/images/no-image.jpg\'; ?>" alt="No image available" class="panikosule" />
                            <?php
                        }
                    }
                    ?>
    
                    <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                    <?php the_excerpt(); ?> 
    
                    </div>
    
                    <?php
                } // endwhile;
            }
        }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    <span class="arrow"></span>
    </div>
    

    SO网友:Mridul Aggarwal

    就在while ($my_query->have_posts()) 添加$flag = false;, 然后将您的特色图像代码包装在另一个if条件中,如下图所示

    if( !$flag ) {
    
    $flag = true;
    
    // put all you featured image code here
    
    }
    
    现在更改post_per_page 参数设置为更高的数字。图像代码仅在每个类别的循环第一次运行时执行

    UPDATE

    <?php
    if( !$flag ) {
        $flag = true;
    ?>
    
                            <!--  Add timthumb to post featured image -->
                            <?php
                            $image_id = get_post_thumbnail_id();
                            $image_url = wp_get_attachment_url($image_id);
                            if ($image_url) {
                            ?>
                            <img src="<?php bloginfo(\'template_directory\'); ?>/scripts/timthumb.php?src=<?php echo $image_url; ?>&w=196&h=115&q=1000" alt="<?php the_title();?>" />
                             <!--  // Add timthumb to post featured image -->
                            <?php } else { ?>
                            <img src="<?php echo bloginfo(\'template_directory\') . \'/images/no-image.jpg\'; ?>" alt="No image available" class="panikosule" />
                            <?php } ?>
    
    
    <?php } ?>
    

    SO网友:user22591

    感谢kaiser和Mridul的回复。我通过以下代码实现了我想要的:

    <div class="categorys_box">
       <!-- Query only the first post -->
       <?php $cat4 = new WP_Query(\'cat=4&showposts=1\');
          if($cat4->have_posts()) : while($cat4->have_posts()) : $cat4->the_post(); ?>
          <!-- First post only content area -->
          <div class="first_post">
    
                    <?php
                    $image_id = get_post_thumbnail_id();
                    $image_url = wp_get_attachment_url($image_id);
                    if ($image_url) {
                     ?>
                    <img src="<?php bloginfo(\'template_directory\'); ?>/scripts/timthumb.php?src=<?php echo $image_url; ?>&w=196&h=115&q=1000" alt="<?php the_title();?>" /> 
                    <!--  // Add timthumb to post featured image -->
                    <?php } else { ?>
                    <img src="<?php echo bloginfo(\'template_directory\') . \'/images/no-image.jpg\'; ?>" alt="No image available" class="panikosule" />
                    <?php } ?>
                    <a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                    </a>
                    <?php the_excerpt(); ?>
    
          </div><!-- //first_post -->
    
          <?php endwhile; endif; ?>
          <!-- Query the rest posts. Offset = -1 from the above  -->
          <?php $sidecat4 = new WP_Query(\'cat=4&showposts=2&offset=1\');
          if($sidecat4->have_posts()) : while($sidecat4->have_posts()) : $sidecat4->the_post(); ?>
           <!-- Rest posts content area -->
         <div class="rest_post">
    
          <a href="<?php the_permalink() ?>">
            <?php the_title() ?>
           </a>
    
            </div><!-- //rest_posts  -->
          <?php endwhile; endif; ?>
          <?php wp_reset_query();  // Restore global post data stomped by the_post().?>
        </div><!-- //categorys_box--> 
    

    结束

    相关推荐

    将筛选器添加到wp_Dropdown_Pages()或wp_Dropdown_Categories()-没有选择容器?

    我在和wp_dropdown_pages() 和wp_dropdown_categories() 并且两者都始终输出<select>-盒子及其<option>s作为项目。有没有可能在这个函数中添加一个过滤器/挂钩,这样我就可以得到<option>s而不被包裹在<select>我这样问的原因是我想将页面和类别合并到一个下拉列表中。我找不到参数来设置这个!有什么想法吗?