WordPress随着4.1的发布,新推出了一个新的主题twentyfifteen,该主题简洁明了,边栏固定,适合博客适用。但是twentyfifteen还是像以前的WordPress官方主题一样,首页以全文的形式展示,这让我很不爽。我希望是首页能以摘要的方式显示。经过搜索发现了解决方法
twentyfifteen主题首页摘要显示方法一
我们知道,在搜索结果中文章是以摘要的形式显示的,因此在index.php中找到以下代码
- <?php
- // Start the loop.
- while ( have_posts() ) : the_post();
- /*
- * Include the Post-Format-specific template for the content.
- * If you want to override 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() );
- // End the loop.
- endwhile;
更改为以下代码。只是把content改为了content-search。
- <?php
- // Start the loop.
- while ( have_posts() ) : the_post();
- /*
- * Include the Post-Format-specific template for the content.
- * If you want to override 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-search’, get_post_format() );
- // End the loop.
- endwhile;
好了,刷新一下首页就可以看到结果了。
twentyfifteen主题首页摘要显示方法二
twentyfifteen首页摘要显示方法二是修改content.php文件。先看看content.php以下部分
- <div class=“entry-content”>
- <?php
- /* translators: %s: Name of current post */
- the_content( sprintf(
- __( ‘Continue reading %s’, ‘twentyfifteen’ ),
- the_title( ‘<span class=“screen-reader-text”>’, ‘</span>’, false )
- ) );
- wp_link_pages( array(
- ‘before’ => ‘<div class=“page-links”><span class=“page-links-title”>’ . __( ‘Pages:’, ‘twentyfifteen’ ) . ‘</span>’,
- ‘after’ => ‘</div>’,
- ‘link_before’ => ‘<span>’,
- ‘link_after’ => ‘</span>’,
- ‘pagelink’ => ‘<span class=“screen-reader-text”>’ . __( ‘Page’, ‘twentyfifteen’ ) . ‘ </span>%’,
- ‘separator’ => ‘<span class=“screen-reader-text”>, </span>’,
- ) );
- ?>
- </div><!– .entry-content-–>
修改成以下代码形式,也就是说除了文章内页以外,全部以摘要的方法显示。
- <div class=“entry-content”>
- <?php
- if ( is_single() ) :
- /* translators: %s: Name of current post */
- the_content( sprintf(
- __( ‘Continue reading %s’, ‘twentyfifteen’ ),
- the_title( ‘<span class=“screen-reader-text”>’, ‘</span>’, false )
- ) );
- wp_link_pages( array(
- ‘before’ => ‘<div class=“page-links”><span class=“page-links-title”>’ . __( ‘Pages:’, ‘twentyfifteen’ ) . ‘</span>’,
- ‘after’ => ‘</div>’,
- ‘link_before’ => ‘<span>’,
- ‘link_after’ => ‘</span>’,
- ‘pagelink’ => ‘<span class=“screen-reader-text”>’ . __( ‘Page’, ‘twentyfifteen’ ) . ‘ </span>%’,
- ‘separator’ => ‘<span class=“screen-reader-text”>, </span>’,
- ) );
- else :
- /* translators: %s: Name of current post */
- the_excerpt( sprintf(
- __( ‘Continue reading %s’, ‘twentyfifteen’ ),
- the_title( ‘<span class=“screen-reader-text”>’, ‘</span>’, false )
- ) );
- wp_link_pages( array(
- ‘before’ => ‘<div class=“page-links”><span class=“page-links-title”>’ . __( ‘Pages:’, ‘twentyfifteen’ ) . ‘</span>’,
- ‘after’ => ‘</div>’,
- ‘link_before’ => ‘<span>’,
- ‘link_after’ => ‘</span>’,
- ‘pagelink’ => ‘<span class=“screen-reader-text”>’ . __( ‘Page’, ‘twentyfifteen’ ) . ‘ </span>%’,
- ‘separator’ => ‘<span class=“screen-reader-text”>, </span>’,
- ) );
- endif;
- ?>
- </div><!– .entry-content-–>
twentyfifteen主题更改摘要字数
虽然经过上面的修改,twentyfifteen主题是以摘要的方法显示了,但是摘要字数并不满意,字数太少。我们可以通过修改functions.php文件来修改摘要显示的字数。找到以下代码
- function twentyfifteen_search_form_modify( $html ) {
- return str_replace( ‘class=“search-submit”‘, ‘class=“search-submit screen-reader-text”‘, $html );
- }
- add_filter( ‘get_search_form’, ‘twentyfifteen_search_form_modify’ );
在其后面加上如下代码。200是摘要所显示的字符数。
- function twenty_fifteen_excerpt_length( $length ) {
- return 200;
- }
- add_filter( ‘excerpt_length’, ‘twenty_fifteen_excerpt_length’, 999 );
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。