【问题标题】:Wordpress Twenty Eleven PhP - forcing php code to skip first post on homepage?Wordpress 21 PhP - 强制 php 代码跳过主页上的第一篇文章?
【发布时间】:2013-11-09 19:28:11
【问题描述】:

我已设置我的 Wordpress 以将 wordpress Featured Image thumbnails 添加到主页上的所有帖子中。

如何让代码跳过将wordpress Featured Image thumbnails 添加到第一个帖子[the_content() ~ 在下面的代码] 中,只将它们添加到wordpress Featured Image thumbnails 其他帖子[the_excerpt() ~ 在下面的代码]?

我在 content.php 中放入的代码使其将wordpress Featured Image thumbnails 放在主页上。 Link Here

    <?php if ( is_search() | is_home() ) : // Edit this to show excerpts in other areas of the theme?>
    <div class="entry-summary">
    <!-- This adds the post thumbnail/featured image -->
        <div class="excerpt-thumb"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
    <?php the_post_thumbnail('excerpt-thumbnail', 'class=alignleft'); ?></a></div>
                      <?php  if($wp_query->current_post == 0 && !is_paged()) { the_content(); } else { the_excerpt(); }     ?>                     


            </div><!-- .entry-summary -->
            <?php else : ?>
            <div class="entry-content">
                    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
            </div><!-- .entry-content -->
            <?php endif; ?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    这非常简单 - 您只需使用与您使用的逻辑相反的逻辑来确定是显示完整内容还是仅显示摘录。

    <?php if ( is_search() || is_home() ) : // Edit this to show excerpts in other areas of the theme?>
        <div class="entry-summary">
            <?php if ( $wp_query->current_post != 0 || is_paged() ) : // Don't display the thumbnail if it's the first post... ?>
                <!-- This adds the post thumbnail/featured image -->
                <div class="excerpt-thumb">
                    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                        <?php the_post_thumbnail('excerpt-thumbnail', 'class=alignleft'); ?>
                    </a>
                </div>
            <?php endif; ?>
            <?php if ( $wp_query->current_post == 0 && ! is_paged() ) {
                the_content();
            } else {
                the_excerpt();
            } ?>
        </div><!-- .entry-summary -->
    <?php else : ?>
        <div class="entry-content">
            <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
            <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
        </div><!-- .entry-content -->
    <?php endif; ?>
    

    PS:我知道这无关紧要,但是请尝试使用更简洁的代码 :) 查看WordPress PHP Coding Standards 以熟悉它们。它使阅读代码变得更加容易。

    【讨论】:

      【解决方案2】:

      定义一个计数器,第一次不显示特色图片。

      <?php $counter++; if ($counter!=1) the_post_thumbnail('excerpt-thumbnail', 'class=alignleft'); ?>
      

      【讨论】:

        【解决方案3】:

        只要验证是不是第一次发帖

        $firstPost = true;
        while() // loop for posts
        if(!$firstPost){
        //your code for adding thumbnail
        }
        else{
        $firstPost = false;
        }
        

        【讨论】:

        • 请不要忘记缩进
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-03
        • 2018-07-22
        相关资源
        最近更新 更多