为WordPress文章添加分页和阅读全文方法

火币

火币HTX

火币是全球三大交易所之一,虚拟货币交易平台安全可靠,注册领取新人礼包!

大多数的WordPress主题都有文章分页功能,或者会有“阅读全文”的功能,今日WordPress主题站就为大家分享为WordPress文章添加分页和阅读全文方法。

图片[1]-玩转网为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=”‘%20.%20get_permalink()%20.%20′”><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 );

}

}

2.到文章模板 single.php 文件的 the_content()下方添加下面的代码调用就可以了。

<?php io_link_pages() ?>

© 版权声明
THE END
点赞21赞赏 分享