Wordpress:使第一篇文章完整,同时留下其余的摘录(带缩略图!)


Wordpress: Making first post full, while leaving the rest excerpts (with thumbnails!)

我有点不懂PHP(我越来越好),但有一件事我遇到了麻烦。我正在努力使我在第一页上的第一篇文章成为一篇完整的文章(没有摘录,也没有缩略图)。在第一篇文章之后,我希望剩下的都是摘录,并附上缩略图(左对齐,100x100像素)。有人能帮我吗?

以下是我在content.php上的内容:

<?php
/**
* @package Spirit
* @since Spirit 1.0
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
    <h7 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( '%s', 'spirit' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h7>
</header><!-- .entry-header -->
<table>
<tbody>
<tr>
<td>
<footer class="entry-meta2" align="left"  title="<?php the_time('g:i A, T') ?>">
    <?php if ( 'post' == get_post_type() ) : ?>
        <?php the_time('l') ?><BR><?php the_time('M jS Y') ?>
    <?php endif; ?>
    <?php if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) : ?>
</footer>
<footer class="entry-meta4" align="left">
    <span class="comments-link"><?php comments_popup_link( __( '0 Comments', 'spirit' ), __( '1 Comment', 'spirit' ), __( '% Comments', 'spirit' ) ); ?></span>
    <?php endif; ?>
</footer>
<footer class="entry-meta3" align="left"  title="category">
<?php
        /* translators: used between list items, there is a space after the comma */
        $category_list = get_the_category_list( __( ', ', 'spirit' ) );

        if ( ! spirit_categorized_blog() ) {
            // This blog only has 1 category so we just need to worry about tags in the meta text
            if ( '' != $tag_list ) {
                $meta_text = __( '<br> tags: %2$s<br><a href="%3$s" title="Permalink to %4$s" rel="bookmark">Permalink</a>', 'spirit' );
            } else {
                $meta_text = __( '<a href="%3$s" title="Permalink to %4$s" rel="bookmark">Permalink</a>.', 'spirit' );
            }
        } else {
            // But this blog has loads of categories so we should probably display them here
            if ( '' != $tag_list ) {
                $meta_text = __( '%1$s <br> tags: %2$s<br><a href="%3$s" title="Permalink to %4$s" rel="bookmark">Permalink</a>', 'spirit' );
            } else {
                $meta_text = __( ' %1$s', 'spirit' );
            }
        } // end check for categories on this blog
        printf(
            $meta_text,
            $category_list,
            $tag_list,
            get_permalink(),
            the_title_attribute( 'echo=0' )
        ); ?>

</footer>

<footer class="entry-meta-two"  title="tag"><?php echo get_the_tag_list('<p><i class="fa fa-tags"></i> ',', ','</p>'); ?></footer>

<footer class="entry-meta" align="left">
<?php edit_post_link( __( '(Edit)', 'spirit' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</td>
<td>

<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">

<?php $excerpt = strip_tags(get_the_excerpt());
    echo $excerpt; ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_post_thumbnail(); ?> <?php $excerpt = strip_tags(get_the_excerpt());
    echo $excerpt; ?>
    <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'spirit' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<!-- .entry-meta -->
</article><!-- #post-<?php the_ID(); ?> -->

以下是我的index.php:中的内容

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package Spirit
 * @since Spirit 1.0
 */
get_header(); ?>

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
        <?php if ( have_posts() ) : ?>
            <?php spirit_content_nav( 'nav-above' ); ?>
            <?php /* Start the Loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php
                    /* Include the Post-Format-specific template for the content.
                     * If you want to overload this in a child theme then include a file
                     * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                     */
                    get_template_part( 'content', get_post_format() );
                ?>
            <?php endwhile; ?>
            <?php spirit_content_nav( 'nav-below' ); ?>
        <?php else : ?>
            <?php get_template_part( 'no-results', 'index' ); ?>
        <?php endif; ?>
        </div><!-- #content .site-content -->
    </div><!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

content.php中的相关部分是以下几行:

<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">

<?php $excerpt = strip_tags(get_the_excerpt());
    echo $excerpt; ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_post_thumbnail(); ?> <?php $excerpt = strip_tags(get_the_excerpt());
    echo $excerpt; ?>
    <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'spirit' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>

由于您没有处于搜索模式,index.php上使用的部分是else条件。要删除帖子缩略图,只需删除the_post_thumbnail(),并使用the_content()获取内容而不是摘录。这是它应该看起来的样子(更新后只影响循环的第一个帖子):

<?php else : ?>
    <div class="entry-content">
    <?php if(!$first): ?>
            <?php the_post_thumbnail(); ?> <?php $excerpt = strip_tags(get_the_excerpt());
            echo $excerpt; ?>
    <?php else: ?>
            <?php the_content(); $first = false; ?>
    <?php endif; ?>
    <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'spirit' ), 'after' => '</div>' ) ); ?>
    </div><!-- .entry-content -->
<?php endif; ?>

index.php中,在循环之前添加以下行:

<?php /* Start the Loop */ ?>
<?php $first = true; ?>
<?php while ( have_posts() ) : the_post(); ?>

我会这样做:

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
    <?php $i = 0; if ( have_posts() ) : ?>
        <?php spirit_content_nav( 'nav-above' ); ?>
        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php
                /* Include the Post-Format-specific template for the content.
                 * If you want to overload this in a child theme then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                if ($i = 0) {
                    get_template_part( 'content_first', get_post_format() );
                    $i++;
                } else{
                    get_template_part( 'content', get_post_format() );
                }
            ?>
        <?php endwhile; ?>
        <?php spirit_content_nav( 'nav-below' ); ?>
    <?php else : ?>
        <?php get_template_part( 'no-results', 'index' ); ?>
    <?php endif; ?>
    </div><!-- #content .site-content -->
</div><!-- #primary .content-area -->

您需要创建一个content_first.php文件,并在那里为您的第一篇文章设置样式,其余部分应该使用content.php模板显示。