• 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.

Share code phân trang wordpress không cần plugin

Admin

Well-Known Member
Staff member
Administrator
1. Phân trang kiểu JohnCMS


Chèn đoạn code này vào file functions.php trong theme của bạn (chỗ nào tùy tâm)
Code:
[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]function wapvn_page_nav() {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    global $wp_query, $paged;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    $total = $wp_query->max_num_pages;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    if(!$total) $total = 1;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    if(empty($paged)) $paged = 1;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    if($total > 1) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        $out = array();[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        if($total > 5) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if($paged > 1) $out[] = '<a class="pagenav" href="' . get_pagenum_link($paged - 1) . '">&lt;&lt;</a>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if($paged > 3) $out[] = '<a class="pagenav" href="' . get_pagenum_link(1) . '">1</a>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if($paged > 4) $out[] = '<span style="font-weight: bold;">&larr;</span>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        for ($i = 1; $i < ($total + 1); $i++) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if (!($i >= ($paged + 3) || $i <= ($paged - 3)) || $total <= 5) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                if ($i == $paged) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                    $out[] = '<span class="currentpage"><b>' . $i . '</b></span>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                } else {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                    $out[] = '<a class="pagenav" href="' . get_pagenum_link($i) . '">' . $i . '</a>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        if ($total > 5) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if ($total > ($paged + 3)) $out[] = '<span style="font-weight: bold;">&rarr;</span>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if ($total > ($paged + 2)) $out[] = '<a class="pagenav" href="' . get_pagenum_link($total) . '">' . $total . '</a>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if ($total > $paged) $out[] = '<a class="pagenav" href="' . get_pagenum_link($paged + 1) . '">&gt;&gt;</a>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        echo '<div class="topmenu">' . implode(' ', $out) . '</div>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]}[/FONT][/COLOR]


Sau đó chèn code này vào nơi hiện thị list bài viết của bạn:
<?php wapvn_page_nav(); ?>
Nếu bạn đã cài wp_pagenavi thì chính chỗ đó đấy.


2. Phân trang bài viết dài kiểu như trên.


Đối với 1 bài viết rất dài, kiểu phân trang mặc định của WordPress rất là xấu, nó hiển thị toàn bộ link page nhìn giống như 1 cái lưới. Bạn có thể dùng cái này để thay thế:


Chèn đoạn code này vào file functions.php trong theme của bạn (chỗ nào tùy tâm):
Code:
[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]function wapvn_link_pages() {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    global $page, $numpages, $multipage;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    if ( $multipage ) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        $out = array();[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        if($numpages > 5) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if($page > 1) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($numpages - 1) . '">&lt;&lt;</a> ';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if($page > 3) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page(1) . '">1</a> ';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if($page > 4) $out[] = '<span style="font-weight: bold;">&larr;</span> ';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        for ($i = 1; $i <= $numpages; $i++) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if (!($i >= ($page + 3) || $i <= ($page - 3)) || $numpages <= 5) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                if ($i == $page) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                    $out[] = '<span class="currentpage"><b>' . $i . '</b></span>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                } else {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                    $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($i) . '">' . $i . '</a>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        if ($numpages > 5) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if ($numpages > ($page + 3)) $out[] = '<span style="font-weight: bold;">&rarr;</span>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if ($numpages > ($page + 2)) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($numpages) . '">' . $numpages . '</a>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if ($numpages > $page) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($page + 1) . '">&gt;&gt;</a>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        echo '<div class="topmenu">' . implode(' ', $out) . '</div>';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]}[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]/**[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]* Hiển thị link[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]* Sử dụng cho wapvn_link_pages()[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]*/[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]function wapvn_get_link_page($pg) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    global $wp_rewrite;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    $post = get_post();[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    if (1 == $pg) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        $url = get_permalink();[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    } else {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        if ('' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')))[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            $url = add_query_arg('page', $pg, get_permalink());[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        elseif ('page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID)[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $pg, 'single_paged');[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        else[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            $url = trailingslashit(get_permalink()) . user_trailingslashit($pg, 'single_paged');[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    return esc_url($url);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]}[/FONT][/COLOR]


Sau đó chèn code này ngay bên dưới cái the_content(); của bạn:
<?php wapvn_link_pages(); ?>
Thay thê cho cái wp_link_pages(); mặc định của WP ấy mà!


À quên đây là đoạn CSS cho Pagenav nhé! Chèn vào file CSS của bạn .


Code:
.topmenu {
Code:
[COLOR=#141414][FONT=Arial]  background-color: #f9d9b0;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  font-size: 11px;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  padding: 4px;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  border: 1px solid #fff;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]}[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]
[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial].currentpage {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  background-color: #f9d9b0;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  border: 1px solid #f9bc6d;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  padding: 0px 5px 0px 5px;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  color: #8f6c3f;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]}[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]
[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]a.pagenav {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  background-color: #f0f7fc;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  border: 1px solid #6cb2e4;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  padding: 0px 5px 0px 5px;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  color: #2b485c;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  text-decoration: none;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]}[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]
[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]a.pagenav:hover {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  background-color: #fff4e5;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  border: 1px solid #f9d9b0;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  color: #8f6c3f;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]}[/FONT][/COLOR]


3. Tự động chia trang bài viết dài theo ký tự:


Cái này để phục vụ cho cái phía trên dành cho các site truyện


Chèn đoạn code này vào cuối cùng của file wp-includes/query.php (hoặc chỗ nào tùy tâm):


Mặc định 500 kí tự / trang - sửa lại theo ý bạn


Code:
[/FONT][/COLOR][COLOR=#141414][FONT=Arial]function wapvn_page_content($content) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    $max_str = 5000; // Số ký tự trên trang[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    $max_page = ceil(mb_strlen($content) / $max_str);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    if ($max_page > 1) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        $content = str_replace('<!--nextpage-->', ' ', $content);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        $tmp_text = array();[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        for ($i = 1; $i <= $max_page; $i++) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            $start_pos = $i == 1 ? 0 : $i * $max_str - $max_str;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            $subtext = mb_substr($content, $start_pos, ($max_str + 150));[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if ($i == 1) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                $int_start = 0;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            } else {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                if (($pos1 = mb_strpos($subtext, ' ')) === false) $pos1 = 150;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                $int_start = $pos1;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            if ($i == $max_page) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                $int_lenght = $max_str;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            } else {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                $tmp = mb_substr($subtext, $max_str, 150);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                if (($pos2 = mb_strpos($tmp, ' ')) === false) $pos2 = 150;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]                $int_lenght = $max_str + $pos2 - $int_start;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]            $tmp_text[] = mb_substr($subtext, $int_start, $int_lenght);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        return implode('<!--nextpage-->', $tmp_text);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    return $content;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]}[/FONT][/COLOR]


Sau đó tại func setup_postdata() tìm đoạn này $content = $post->post_content; (nó ở ngay trên thui, dưới cùng của file query.php ấy:
Sửa:
Code:
$content = $post->post_content;
Thành:
Code:
$content = wapvn_page_content($post->post_content);


Live Demo:


- Phân trang Page Nav kiểu JohnCMS: Xem demo (ít bài viết để 2bv / trang để các bạn nhìn toàn cảnh Pagenav)


- Phân trang bài viết dài Auto chia trang theo kí tự: Xem demo


Nguồn: QDK
 

Facebook Comments

Similar threads
Thread starter Title Forum Replies Date
Smobi Share Code phân trang bằng Ajax đơn giản PHP 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 php phân trang Mã nguồn wap 8
D Share Code phân trang cho xtgem All Shared Scripts 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 code phân trang cho xtgem Wap builder, wapego, xtgem, wen.ru, wapka, wap4 0
Admin Share code phân tích bbcode Mã nguồn web 0
rockymen9999 Share GIFTCARD CODE GENERATOR BY AMBOSS NETFLIX, AMAZON, PSN, SPOTIFY & MORE Tut, tool, mmo 0
F Quét QR Code – 1 Chạm 1 Lượt Tải Tốc Độ Cao Tin tức CNTT 0
rockymen9999 Share GIFTCARD CODE GENERATOR BY AMBOSS NETFLIX, AMAZON, PSN, SPOTIFY & MORE Tut, tool, mmo 0
J E cần tối ưu code check cc,web check ạ PHP 0
cuongpro9x Share FREE Windscribe VPN 30GB Voucher Code Tut, tool, mmo 0
H Xin code twig lô đề All Shared Scripts 0
katy Android MobiFone tặng e-code 100k cho Hội viên KNDL để mua sắm trên Tiki.vn Điện thoại di động 10
katy Android Hãy cùng MobiFone thăng hạng tiếng Anh cùng e-code giảm giá tại Apax English Điện thoại di động 10
katy Android MobiFone tặng mã e-code tại APAX ENGLISH trị giá 500k cho Hội viên KNDL Thông tin các mạng di động 0
Admin [CSCN] Roman Numeral Chord BB Code Support Xenforo 0
Admin Editor & BB Code Manager Xenforo 0
Admin Share code phpBB 3.3.3 tiếng việt mới nhất Phpbb3x 2
D Giúp đỡ về code javascrip trên Ladipage Hỗ trợ sử dụng diễn đàn 1
cuongpro9x Share Code vòng quay lì xì may mắn All Shared Scripts 1
Admin Xenforo 2 - Random Ads Code Xenforo 0
Admin Yilmaz - Hide [code,php,html] content from guests (vb5.6.x) Add-ons 0
Vtkun Xin giải mã code php Thảo luận chung 0
Admin Share code dịch vụ mạng xã hội bao gồm Facebook, Youtube, Tiktok, Instagram Mã nguồn wap 30
Admin Share 15GB source code Android bao gồm Unity Android, iOS 5
Admin Share code Nukeviet 4.4.02 mới nhất - Code tin tức tốt nhất Việt Nam Mã nguồn web 3
Admin Share code trung thu 2020 đẹp mắt JavaScript / Ajax 6
Admin [MMO] Hide Bb-Code Content System Xenforo 1
I Xin Xin code up ảnh giống upanh.tuoitreit.vn PHP 0
Admin Share code phpBB 3.3.1 tiếng việt mới nhất Phpbb3x 4
Admin Share code wap upload mod full Mã nguồn wap 3
Admin Share code bắn pháo hoa tết 2021 đẹp JavaScript / Ajax 0
cuongpro9x Share bộ Source code trà sữa To Co To Co Wordpress 9
B xin lại các code link đã bị die Johncms 2
cuongpro9x Share code website bán khóa học Online All Shared Scripts 3
Admin Share code php check rank Việt Nam và thế giới từ Alexa PHP 0
Admin Share code leech ảnh girl xinh tự động Mã nguồn wap 0
cuongpro9x Share Code Đếm Ngược Thời Gian Chào Xuân Canh Tý 2020 All Shared Scripts 3
T Help Cần code web nghe nhạc Mã nguồn wap 0
cuongpro9x Share Code Flarum Đã Config Sẵn All Shared Scripts 6
cuongpro9x Share Full code WordPress Bán Hàng Giống Shopee, Lazada Wordpress 57
cuongpro9x Share code forum xenforo 2 Xenforo 146
Admin eBay Search function - code updated vBulletin Add-ons 0
N Share CCV CHECKER CHK v1.0 [SOURCE CODE VB .NET] Hacking 0
Admin How to add amp adsense code to xenforo 2 simple Xenforo 0
Admin [SC] BB Code YouTube xenforo 2 Xenforo 0
Admin Share code và data game bài Gowin 2019 mới nhất Mã nguồn web 9
Admin How to convert bbcode hide hack from [Aayush] Hide Hack v1 to Editor & BB Code Manager Xenforo 0

Similar threads

New posts New threads New resources

Back
Top