WordPress文章设置分页和阅读全文选项
WordPress文章为了给用户在阅读上给予优质的体验,除了基础的将文章分成1至3页外,还需要有:阅读全文、和更多分页的选项。以下为WordPress文章添加分页和阅读全文方法。
添加函数
复制下方代码到主题的 functions.php 文件里面:
function io_link_pages() {
global $wp_query, $numpages;
if(isset( $wp_query->query_vars[ 'view' ] ) && $wp_query->query_vars[ 'view' ] === 'all'){
echo '<div class="page-nav text-center my-3"><a href="' . get_permalink() . '"><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_match( '/\?/', get_pagenum_link( 1 ) ) ? '&' : '?' ) . 'view=all' . '"><span class="all">阅读全文</span></a></div>';
}
}
add_filter( 'query_vars', 'wp_link_pages_all_parameter_queryvars' );
add_action( 'the_post', 'wp_link_pages_all_the_post' , 0 );
function wp_link_pages_all_parameter_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' ] ) && ( '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() ?>
0条评论