Wordpress - 仅在循环中的第一个帖子上显示缩略图



我是Wordpress的新手,我想知道如何仅在每个类别的第一个条目中显示帖子的图像,而其他条目仅显示标题和描述,我目前有以下代码:

    <?php if ( have_posts() ) : ?>
    <section>
    <?php echo do_shortcode("[soliloquy slug='categorias']"); ?>      
<div style="clear:both; height:10px; margin:0 0 20px;"></div>
<?php while ( have_posts() ) : the_post(); ?> 
    <article>
      <header>
       <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <time datatime="<?php the_time('F j, Y'); ?>"><i class="fa fa-clock-o"></i>  <?php the_time('j F, Y'); ?> &nbsp; | &nbsp; Publicado en: <?php the_category(', ') ?></time>
            </header>
            <figure class="category">
                    <a href="<?php the_permalink(); ?>" rel="bookmark" title="Leer la nota <?php the_title_attribute(); ?>">
                        <?php
                        // Must be inside a loop.
                        if ( has_post_thumbnail( $_post->ID ) ) {
                        echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . the_title_attribute( array( 'echo' => 0 ) ) . '">';
                        echo get_the_post_thumbnail( $_post->ID, 'medium' );
                        echo '</a>';
                        }
                        else {
                        echo '<img src="http://www.changoonga.com/wp-content/uploads/2016/10/no-foto.jpg" title="no hay foto" />';
                        } ?>
                    </a>
            </figure>
            <div class="cat-excerpt"><?php the_excerpt(); ?></div>
            <div style="clear:both; height:0px; margin:0"></div>
          </article>
        <?php endwhile; ?>
        <div class="pagination">
          <span><?php next_posts_link('« Notas anteriores'); ?></span>
          <span><?php previous_posts_link('Notas recientes »'); ?></span>
        </div>
    </section>
    <?php else : ?>
      <p><?php _e('Ups!, no hay entradas.'); ?></p>
    <h2>Puedes ver lo más reciente o intentar con otra búsqueda</h2>
    <?php echo do_shortcode("[display-posts posts_per_page='10' image_size='medium' include_excerpt='true']"); ?>

    <?php endif; ?>

对不起,我的英语不好!

我会添加一个$i来检查帖子的数量。在循环添加之前。

    <?php $i = 1; ?>

然后将您的替换为

    <?php if($==1) { ?>
        <figure class="category">
            <a href="<?php the_permalink(); ?>" rel="bookmark" title="Leer la nota <?php the_title_attribute(); ?>">
            <?php
                // Must be inside a loop.
                if ( has_post_thumbnail( $_post->ID ) ) {
                    echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . the_title_attribute( array( 'echo' => 0 ) ) . '">';
                    echo get_the_post_thumbnail( $_post->ID, 'medium' );
                    echo '</a>';
                }
                else {
                    echo '<img src="http://www.changoonga.com/wp-content/uploads/2016/10/no-foto.jpg" title="no hay foto" />';
                } ?>
            </a>
        </figure>
    <?php } ?>

最后替换这个:

    <?php $++; endwhile; ?>

这是一个循环,您只需检查第一篇文章。

祝你好运!

最新更新