一般WordPress主题都会带有文章分页功能,但是很少有“阅读全文”的功能,如果分页少没有该功能是无所谓的,但是如果分页过多的话,为了提高用户体验,还是需要添加一个“阅读全文”的功能。那么WordPress如何实现添加分页及阅读全文功能?
**下方代码到主题的 functions.php 文件里面:
function io_link_pages() {
global $wp_query, $numpages;
if(isset( $wp_query->query_vars[ ‘view’ ] ) &**p;&**p; $wp_query->query_vars[ ‘view’ ] === ‘all’){
echo ‘<div class=”page-nav text-center my-3″><a href=”‘ . get_per**link() . ‘”><span class=”all”>分页阅读</span></a></div>’;
} elseif ( 1 < $numpages ) {
wp_link_pages(array(‘before’ => ‘<div class=”page-nav text-center my-3″>’, ‘after’ => ”, ‘next_or_number’ => ‘next’, ‘previouspagelink’ => ‘<span><i class=”iconfont icon-arrow-l”></i></span>’, ‘nextpagelink’ => “”));
wp_link_pages(array(‘before’ => ”, ‘after’ => ”, ‘next_or_number’ => ‘number’, ‘link_before’ =>'<span>’, ‘link_after’=>'</span>’));
wp_link_pages(array(‘before’ => ”, ‘after’ => ”, ‘next_or_number’ => ‘next’, ‘previouspagelink’ => ”, ‘nextpagelink’ => ‘ <span><i class=”iconfont icon-arrow-r”></i></span>’));
echo ‘ <a href=”‘ . get_pagenum_link( 1 ) . ( preg_**tch( ‘/\?/’, get_pagenum_link( 1 ) ) ? ‘&**p;’ : ‘?’ ) . ‘view=all’ . ‘”><span class=”all”>阅读全文</span></a></div>’;
}
}
add_filter( ‘query_vars’, ‘wp_link_pages_all_par**eter_queryvars’ );
add_action( ‘the_post’, ‘wp_link_pages_all_the_post’ , 0 );
function wp_link_pages_all_par**eter_queryvars( $queryvars ) {
$queryvars[] = ‘view’;
return( $queryvars );
}
function wp_link_pages_all_the_post( $post ) {
global $pages, $multipage, $wp_query;
if ( isset( $wp_query->query_vars[ ‘view’ ] ) &**p;&**p; ( ‘all’ === $wp_query->query_vars[ ‘view’ ] ) ) {
$multipage = true;
$post->post_content = str_replace( ‘<!–nextpage–>’, ”, $post->post_content );
$pages = array( $post->post_content );
}
}
到文章模板 single.php 文件的the_content()下方添加下面的代码调用就可以了。
<?php io_link_pages() ?>
暂无评论内容