«

Twenty Fifteen主题首页摘要显示方法

时间:2021-8-2 15:18     作者:lijiayan     分类: 分享



Twenty Fifteen主题首页摘要显示方法一


我们知道,在搜索结果中文章是以摘要的形式显示的,因此在index.php中找到以下代码


  1. <?php
  2. // Start the loop.
  3. while ( have_posts() ) : the_post();
  4. /*
  5. * Include the Post-Format-specific template for the content.
  6. * If you want to override this in a child theme, then include a file
  7. * called content-___.php (where ___ is the Post Format name) and that will be used instead.
  8. */

  9. get_template_part( 'content', get_post_format() );
  10. // End the loop.
  11. endwhile;


更改为以下代码。只是把content改为了content-search



  1. <?php
  2. // Start the loop.
  3. while ( have_posts() ) : the_post();
  4. /*
  5. * Include the Post-Format-specific template for the content.
  6. * If you want to override this in a child theme, then include a file
  7. * called content-___.php (where ___ is the Post Format name) and that will be used instead.
  8. */

  9. get_template_part( 'content-search', get_post_format() );
  10. // End the loop.
  11. endwhile;


好了,刷新一下首页就可以看到结果了。


twentyfifteen主题首页摘要显示方法二


twentyfifteen首页摘要显示方法二是修改content.php文件。先看看content.php以下部分



  1. <div class="entry-content">

  2. <?php
  3. /* translators: %s: Name of current post */

  4. the_content( sprintf(

  5. __( 'Continue reading %s', 'twentyfifteen' ),

  6. the_title( '<span class="screen-reader-text">', '</span>', false )

  7. ) );

  8. wp_link_pages( array(

  9. 'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',

  10. 'after'       => '</div>',

  11. 'link_before' => '<span>',

  12. 'link_after'  => '</span>',

  13. 'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',

  14. 'separator'   => '<span class="screen-reader-text">, </span>',

  15. ) );

  16. ?>

  17. </div><!-- .entry-content-–>


修改成以下代码形式,也就是说除了文章内页以外,全部以摘要的方法显示。



  1. <div class="entry-content">

  2. <?php
  3. if ( is_single() ) :
  4. /* translators: %s: Name of current post */

  5. the_content( sprintf(

  6. __( 'Continue reading %s', 'twentyfifteen' ),

  7. the_title( '<span class="screen-reader-text">', '</span>', false )

  8. ) );

  9. wp_link_pages( array(

  10. 'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',

  11. 'after'       => '</div>',

  12. 'link_before' => '<span>',

  13. 'link_after'  => '</span>',

  14. 'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',

  15. 'separator'   => '<span class="screen-reader-text">, </span>',

  16. ) );
  17. else :
  18. /* translators: %s: Name of current post */

  19. the_excerpt( sprintf(

  20. __( 'Continue reading %s', 'twentyfifteen' ),

  21. the_title( '<span class="screen-reader-text">', '</span>', false )

  22. ) );

  23. wp_link_pages( array(

  24. 'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',

  25. 'after'       => '</div>',

  26. 'link_before' => '<span>',

  27. 'link_after'  => '</span>',

  28. 'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',

  29. 'separator'   => '<span class="screen-reader-text">, </span>',

  30. ) );
  31. endif;

  32. ?>

  33. </div><!-- .entry-content-–>


twentyfifteen主题更改摘要字数


虽然经过上面的修改,twentyfifteen主题是以摘要的方法显示了,但是摘要字数并不满意,字数太少。我们可以通过修改functions.php文件来修改摘要显示的字数。找到以下代码


  1. function twentyfifteen_search_form_modify( $html ) {
  2. return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );

  3. }

  4. add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );


在其后面加上如下代码。200是摘要所显示的字符数


  1. function twenty_fifteen_excerpt_length( $length ) {
  2. return 200;

  3. }

  4. add_filter( 'excerpt_length', 'twenty_fifteen_excerpt_length', 999 );

标签: Twenty Fifteen WordPress主题

推荐阅读:

评论:
雷达探币 2021-11-07 04:39
旧时光是个哑巴,唱不出我的天涯海角
马内 2021-08-09 11:18
感谢博主的分享,支持了。
技术文章,学习了。