Wordpress:让第一篇文章完整,同时留下其他摘录(缩略图!)

时间:2015-11-09 14:03:45

标签: php wordpress 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(); ?>

2 个答案:

答案 0 :(得分:0)

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(); ?>

答案 1 :(得分:0)

我这样做:

<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模板显示。