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

大多数的WordPress主题都有文章分页功能,或者会有“阅读全文”的功能,今日VPSCHE小车博客就为大家分…

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

为WordPress文章添加分页和阅读全文方法 (https://www.yunsxr.com/) WordPress开发教程 第1张

添加函数

复制下方代码到主题的 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() ?>

类别:WordPress技巧

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT