• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Hướng dẫn phân trang bài viết wordpress

Admin

Well-Known Member
Staff member
Administrator
Phân bằng bằng file wp-includes/query.php
đầu tiên bạn mở wp-includes/query.php:
Tìm đoạn
PHP:
/**
* Set up global post data.
*
* @since 1.5.0
*
* @param object $post Post data.
* @uses do_action_ref_array() Calls 'the_post'
* @return bool True when finished.
*/
function setup_postdata( $post ) {
    global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;

    $id = (int) $post->ID;

    $authordata = get_userdata($post->post_author);

    $currentday = mysql2date('d.m.y', $post->post_date, false);
    $currentmonth = mysql2date('m', $post->post_date, false);
    $numpages = 1;
    $multipage = 0;
    $page = get_query_var('page');
    if ( ! $page )
        $page = 1;
    if ( is_single() || is_page() || is_feed() )
        $more = 1;
    $content = $post->post_content;
    if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
        if ( $page > 1 )
            $more = 1;
        $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
        $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
        $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
        // Ignore nextpage at the beginning of the content.
        if ( 0 === strpos( $content, '<!--nextpage-->' ) )
            $content = substr( $content, 15 );
        $pages = explode('<!--nextpage-->', $content);
        $numpages = count($pages);
        if ( $numpages > 1 )
            $multipage = 1;
    } else {
        $pages = array( $post->post_content );
    }

    do_action_ref_array('the_post', array(&$post));

    return true;
}

Thay đoạn đó bằng
PHP:
/**
* Set up global post data.
*
* @since 1.5.0
*
* @param object $post Post data.
* @uses do_action_ref_array() Calls 'the_post'
* @return bool True when finished.
*/
function setup_postdata($post) {
    global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;

    $id = (int) $post->ID;

    $authordata = get_userdata($post->post_author);

    $currentday = mysql2date('d.m.y', $post->post_date, false);
    $currentmonth = mysql2date('m', $post->post_date, false);
    $numpages = 1;
    $page = get_query_var('page');
    if ( !$page )
        $page = 1;
    if ( is_single() || is_page() || is_feed() )
        $more = 1;
    $content = $post->post_content;
    if ( strpos( $content, '<!--nextpage-->' ) ) {
        if ( $page > 1 )
            $more = 1;
        $multipage = 1;
        $pages = explode('<!--nextpage-->', $content);
        $numpages = count($pages);
    } else {
        ini_set('memory_limit', '500M');
        $max_lex = 5000;
        $string = $content;
        $strrpos = mb_strrpos($content," ");
        $current = 0;
        $pagesPost = ceil($strrpos/$max_lex);
        $pagePost = 1;
        if ($strrpos > $max_lex) {
            $content = '';
            while ($current < $strrpos) {
            $stringtt = mb_substr($string, $current, $max_lex);
            $numStr = mb_strrpos($stringtt," ");
            if($pagePost<$pagesPost)  $content .=  $stringtt."<!--nextpage-->";  else $content .= $stringtt;
            $current = $numStr + $current;
            $pagePost++;
            }
        } else {
        $content = $string;
    }
        if ( $page > 1 ) $more = 1;
        $multipage = 1;
        $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
        $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
        $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
        $pages = explode('<!--nextpage-->', $content);
        $numpages = count($pages);
        /*
        $pages = array( $post->post_content );
        $multipage = 0;
        */
    }

    do_action_ref_array('the_post', array(&$post));

    return true;
}
Trong đó:

$max_lex = 5000; //Số ký tự bạn muốn trong một trang

Cho code này vào chỗ bạn muốn để phân trang :D
PHP:
<?php link_pages('<p>Trang:</strong> ', '</p>', 'number'); ?>
Về css thì bạn tự chỉnh nhé :)

Phần tiếp theo là mình hướng dẫn các bạn rút gọn cho đẹp :D
* Phần này làm theo của QDK nhé :)
Các bạn mở file funtions.php của theme
Thêm đoạn này vào
PHP:
<?php
function wapvn_link_pages() {
    global $page, $numpages, $multipage;
    if ( $multipage ) {
        $out = array();
        if($numpages > 5) {
            if($page > 1) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($numpages - 1) . '">&lt;&lt;</a> ';
            if($page > 3) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page(1) . '">1</a> ';
            if($page > 4) $out[] = '<span style="font-weight: bold;">&larr;</span> ';
        }
        for ($i = 1; $i <= $numpages; $i++) {
            if (!($i >= ($page + 3) || $i <= ($page - 3)) || $numpages <= 5) {
                if ($i == $page) {
                    $out[] = '<span class="currentpage"><b>' . $i . '</b></span>';
                } else {
                    $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($i) . '">' . $i . '</a>';
                }
            }
        }
        if ($numpages > 5) {
            if ($numpages > ($page + 3)) $out[] = '<span style="font-weight: bold;">&rarr;</span>';
            if ($numpages > ($page + 2)) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($numpages) . '">' . $numpages . '</a>';
            if ($numpages > $page) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($page + 1) . '">&gt;&gt;</a>';
        }
        echo '<div class="topmenu">' . implode(' ', $out) . '</div>';
    }
}
function wapvn_get_link_page($pg) {
    global $wp_rewrite;
    $post = get_post();
    if (1 == $pg) {
        $url = get_permalink();
    } else {
        if ('' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')))
            $url = add_query_arg('page', $pg, get_permalink());
        elseif ('page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID)
            $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $pg, 'single_paged');
        else
            $url = trailingslashit(get_permalink()) . user_trailingslashit($pg, 'single_paged');
    }
    return esc_url($url);
}
?>

Sau đó dùng
PHP:
<?php wapvn_link_pages(); ?>
Thay vào chỗ mà bạn để
PHP:
<?php link_pages('<p>Trang:</strong> ', '</p>', 'number'); ?>

Tiếp là trang trí css
Mở Style.css
Thêm:
Code:
.topmenu {
  background-color: #f9d9b0;
  font-size: 11px;
  padding: 4px;
  border: 1px solid #fff;
}

.currentpage {
  background-color: #f9d9b0;
  border: 1px solid #f9bc6d;
  padding: 0px 5px 0px 5px;
  color: #8f6c3f;
}

a.pagenav {
  background-color: #f0f7fc;
  border: 1px solid #6cb2e4;
  padding: 0px 5px 0px 5px;
  color: #2b485c;
  text-decoration: none;
}

a.pagenav:hover {
  background-color: #fff4e5;
  border: 1px solid #f9d9b0;
  color: #8f6c3f;
}

Màu sắc css thì các bạn có thể tự chỉnh theo ý nhé
Demo: http://anhvang.biz/truyen-teen-em-khong-vao-dia-nguc-thi-ai-vao-full.html

Lưu ý nhé: Khi bạn nâng cấp lên ver WP thì phải làm lại nhé :D
 
Em post đủ 20 top có ích thì pm anh, không comment lung tung nhé
 

Facebook Comments

Similar threads
Thread starter Title Forum Replies Date
Tuzoro Share Tự động phân trang bài viết WordPress khi quá dài Wordpress 0
Admin Share plugin tự động phân trang bài viết wordpress Wordpress 1
Admin Hướng dẫn tự động phân trang bài viết wordpress khi quá dài Wordpress 0
Admin Hướng dẫn phân trang bảng PHP sử dụng MySQLi PHP 0
Smobi Share Code phân trang bằng Ajax đơn giản PHP 0
V Help Phân Trang Xtgem Wap builder, wapego, xtgem, wen.ru, wapka, wap4 0
Admin Share code blog xtgem mod bản mới fix lỗi phân trang Wap builder, wapego, xtgem, wen.ru, wapka, wap4 0
Admin Share code phân trang wordpress không cần plugin Wordpress 0
Admin Share phân trang mỗi topic ở trang chủ wapka Wap builder, wapego, xtgem, wen.ru, wapka, wap4 0
Admin Share xtscript phân trang không lỗi Wap builder, wapego, xtgem, wen.ru, wapka, wap4 0
Admin Share code php phân trang Mã nguồn wap 8
D Share Code phân trang cho xtgem All Shared Scripts 0
T Share Phân trang jonhcms Johncms 0
H Hướng dẫn tạo code phân trang trong PHP cực đơn giản + Trả Lời Ðề Tài Kiến thức lập trình 0
Admin Hướng dẫn tạo code phân trang trong php cực đơn giản PHP 0
Admin Share filelist truyện có dấu, tự lấy mô tả, phân trang Wap builder, wapego, xtgem, wen.ru, wapka, wap4 1
L Share Filelist truyện có dấu-phân trang Xtgem Wap builder, wapego, xtgem, wen.ru, wapka, wap4 1
Admin Share code phân trang cho xtgem Wap builder, wapego, xtgem, wen.ru, wapka, wap4 0
Admin Phân tích ưu điểm và nhược điểm của sàn FIBO Group Tut, tool, mmo 0
Admin Hướng dẫn tự động phân cách phần nghìn khi nhập số C# giống tiền C# / C++ 0
T Google tạo ra công cụ để phân tích blockchain của Ethereum Tin tức CNTT 0
P Phần mềm PC AOMEI Partition Assistant Professional 6.3.0 Multilingual - Quản lý phân vùng ổ cứng Phần mềm 0
N Phân nhóm phục vụ khách hàng gọi tới Hotline 18001090 và 9090 Thông tin các mạng di động 3
B MobiFone chính thức phân phối điện thoại iPhone 7 và iPhone 7 Plus Thông tin các mạng di động 7
V mobifone phân phối bán máy iphone giá rẻ Thông tin các mạng di động 6
hunglata Hỏi help phân vùng ổ cứng Hệ điều hành 5
Admin Tổng hợp 45 đề tài quản lý phân tích thiết kế hệ thống chi tiết Sách, truyện, tài liệu 0
Admin Hướng dẫn phân biệt VPS công nghệ ảo hóa OpenVZ, KVM, XEN chi tiết VPS & Dedicated Server 2
T Phân phối Máy Mài Góc Makita 9067 180mm chính hãng ở Miền Bắc và Đắk Nông Trò chuyện linh tinh 0
C 20 cách phân biệt gái ngoan và gái hư Tin sock, độc, lạ 8
Admin Cách sử dụng và phân biệt will và going to chi tiết Sách, truyện, tài liệu 0
Admin Hướng dẫn phân vùng ổ cứng cho N9 Android, ios, java, windows phone 0
Admin Tablet màn hình 20" độ phân giải 4K có giá 6.000 USD tại Mỹ, tháng 1/2014 bán Tin tức, giới thiệu về ĐTDĐ 0
Admin Google chính thức phân phối Hangouts 2.0 cho Android, tích hợp khả năng gửi nhận SMS/MMS Tin tức CNTT 0
Admin HTC phân phối trở lại One X 16GB: giá 7,6 triệu, giao diện Sense 5 Tin tức CNTT 1
K Cách phân biệt "some time", "sometime", "sometimes" Góc thành viên học tập 0
Admin Hướng dẫn tối ưu hóa xenforo phần 2 - Phân mảnh trong MySQL và cách sửa chữa Xenforo 0
L chuyên phân phối xe đạp điện honda giá rẻ tại thanh xuân Trò chuyện linh tinh 0
T Hỏi chuyên phân phối máy kích điện giá cực rẻ giao hàng thu tiền tận nơi Mã nguồn web 0
D Hướng dẫn phân phối loa karaoke mini giá rẻ nhất mạng Máy tính 0
H phân phối linh kiện máy tính ở bình dương Truyện cười 1
H Phân phối thiết bị mạng TPLINK giá rẻ nhất thị trường,bảo hành 24 tháng Trò chuyện linh tinh 2
T S40 [Việt hóa] Tam Quốc Phân Tranh 6 Crack, hack, mod, ghép game, ứng dụng 0
L Bán buôn đồ lót xuất khẩu - Tìm nhà phân phối tỉnh Trò chuyện linh tinh 0
T Bán buôn đồ lót xuất khẩu - Tìm nhà phân phối tỉnh Trò chuyện linh tinh 0
L Share phân phối loa karaoke mini giá rẻ sốc hát tẹt ga JSP 0
V Thảo luận phân phối loa karaoke mini giá rẻ sốc hát tẹt ga Wordpress 0
L Phân phối xe đạp điện kiểu dáng đẹp giá rẻ Truyện cười 0
T Hướng dẫn Máy phân tích nước tiểu MISSION U120 giá sỉ Xenforo 0
I Windows 8.1 hỗ trợ màn hình độ phân giải cao tốt hơn Tin tức CNTT 0

Similar threads

New posts New threads New resources

Back
Top