如何在WordPress首页仅显示带分页的文章标题与特色图片
解决Twenty Fifteen主题首页仅显示文章标题+特色图片并带分页的方法
方法一:直接修改index.php模板(建议先备份文件)
找到你提供的index.php代码中的循环部分,将get_template_part( 'content', get_post_format() );替换为以下代码,直接输出特色图片和文章标题:
// 输出文章特色图片(带链接到文章详情) if ( has_post_thumbnail() ) { echo '<div class="post-featured-image">'; echo '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">'; the_post_thumbnail( 'post-thumbnail' ); echo '</a>'; echo '</div>'; } // 输出文章标题(带链接) echo '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . get_the_title() . '</a></h2>';
然后在循环结束(endwhile;)之后添加分页导航代码,确保分页功能正常:
// 显示分页导航 the_posts_pagination( array( 'mid_size' => 2, 'prev_text' => __( '上一页', 'twentyfifteen' ), 'next_text' => __( '下一页', 'twentyfifteen' ), ) );
方法二:使用子主题创建专属模板(推荐,避免主题更新丢失修改)
- 若未创建子主题,先完成Twenty Fifteen子主题的创建并启用(可参考WordPress官方文档的子主题创建流程)。
- 在子主题目录下新建
content-home.php文件,写入以下内容:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php if ( has_post_thumbnail() ) : ?> <div class="post-thumbnail"> <a href="<?php esc_url( the_permalink() ); ?>" rel="bookmark"> <?php the_post_thumbnail(); ?> </a> </div> <?php endif; ?> <header class="entry-header"> <?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?> </header> </article>
- 修改子主题的
index.php文件,将循环中的get_template_part( 'content', get_post_format() );替换为:
if ( is_home() ) { get_template_part( 'content', 'home' ); } else { get_template_part( 'content', get_post_format() ); }
最后确保分页生效
进入WordPress后台,依次点击「设置」→「阅读」,在「博客页面最多显示」选项中设置每页显示的文章数量(比如10篇),保存设置后首页就会自动分页。
内容的提问来源于stack exchange,提问作者user366810
相关产品推荐
相关产品推荐

