当前位置:首页>网站建设>纯代码给WordPress文章添加分页和阅读全文

纯代码给WordPress文章添加分页和阅读全文

释放双眼,带上耳机,听听看~!

文章分页的功能大多数的 WordPress 主题都有,但是很少有“阅读全文”的功能,相关教程网上也有,这里分享我的改良版。

纯代码给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 );
    }
}

调用

 

欢迎访问秀主题博客,分享简单实用WP教程

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

<?php io_link_pages() ?>
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
网站建设

阻止用邮件不停注册wordpress账户的方法

2024-7-13 7:43:56

网站建设

7b2圈子顶部添加文字滚动通知

2024-7-15 9:22:07

温馨提示:

1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:971318156@qq.com,我们将第一时间处理!

2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。

3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索