Twenty Fifteen主题首页摘要显示方法
Twenty Fifteen主题首页摘要显示方法一
我们知道,在搜索结果中文章是以摘要的形式显示的,因此在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 );
推荐阅读:
收到2条评论
雷达探币 2021-11-07 04:39
旧时光是个哑巴,唱不出我的天涯海角
回复
马内 2021-08-09 11:18
感谢博主的分享,支持了。
技术文章,学习了。
技术文章,学习了。
回复