XtGem Forum catalog

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

Share code phân trang wordpress không cần plugin - www.goc4u.ug

Chào các bạn!

Mình thì không hứng thú mấy tới cái WordPress. Hum qua ngồi viết nghịch cái theme mobile cho nó, viết ra mấy cái này chia sẻ các bạn (cho ai cần)

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)

[php]/
* Phân trang Page Nav giống JohnCMS
* Code bởi Quyetdaik
*/
function wapvn_page_nav() {
global $wp_query, $paged;
$total = $wp_query->max_num_pages;
if(!$total) $total = 1;
if(empty($paged)) $paged = 1;
if($total > 1) {
$out = array();
if($total > 5) {
if($paged > 1) $out[] = '<<';
if($paged > 3) $out[] = '1';
if($paged > 4) $out[] = '';
}
for ($i = 1; $i < ($total + 1); $i++) {
if (!($i >= ($paged + 3) || $i <= ($paged - 3)) || $total <= 5) {
if ($i == $paged) {
$out[] = '<span class="currentpage">' . $i . '';
} else {
$out[] = '' . $i . '';
}
}
}
if ($total > 5) {
if ($total > ($paged + 3)) $out[] = '';
if ($total > ($paged + 2)) $out[] = '' . $total . '';
if ($total > $paged) $out[] = '>>';
}
echo '

' . implode(' ', $out) . '
';
}
}[/php]

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):

[php]/

* Phân trang bài viết dài nếu <!--nextpage-->
* Code bởi Quyetdaik
*/
function wapvn_link_pages() {
global $page, $numpages, $multipage;
if ( $multipage ) {
$out = array();
if($numpages > 5) {
if($page > 1) $out[] = '<< ';
if($page > 3) $out[] = '1 ';
if($page > 4) $out[] = ' ';
}
for ($i = 1; $i <= $numpages; $i++) {
if (!($i >= ($page + 3) || $i <= ($page - 3)) || $numpages <= 5) {
if ($i == $page) {
$out[] = '<span class="currentpage">' . $i . '';
} else {
$out[] = '' . $i . '';
}
}
}
if ($numpages > 5) {
if ($numpages > ($page + 3)) $out[] = '';
if ($numpages > ($page + 2)) $out[] = '' . $numpages . '';
if ($numpages > $page) $out[] = '>>';
}
echo '
' . implode(' ', $out) . '
';
}
}

/**
* Hiển thị link
* Sử dụng cho wapvn_link_pages()
*/
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);
}[/php]

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 .

[php].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;
}[/php]

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 (hoặc chỗ nào tùy tâm) của file wp-includes/query.php :

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

[php]// Phân trang bài viết dài bởi Quyetdaik
function wapvn_page_content($content) {
$max_str = 5000; // Số ký tự trên trang
$max_page = ceil(mb_strlen($content) / $max_str);
if ($max_page > 1) {
$content = str_replace('<!--nextpage-->', ' ', $content);
$tmp_text = array();
for ($i = 1; $i <= $max_page; $i++) {
$start_pos = $i == 1 ? 0 : $i * $max_str - $max_str;
$subtext = mb_substr($content, $start_pos, ($max_str + 150));
if ($i == 1) {
$int_start = 0;
} else {
if (($pos1 = mb_strpos($subtext, ' ')) === false) $pos1 = 150;
$int_start = $pos1;
}
if ($i == $max_page) {
$int_lenght = $max_str;
} else {
$tmp = mb_substr($subtext, $max_str, 150);
if (($pos2 = mb_strpos($tmp, ' ')) === false) $pos2 = 150;
$int_lenght = $max_str + $pos2 - $int_start;
}
$tmp_text[] = mb_substr($subtext, $int_start, $int_lenght);
}
return implode('<!--nextpage-->', $tmp_text);
}
return $content;
}[/php]

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: [php]$content = $post->post_content;[/php]
Thành: [php]$content = wapvn_page_content($post->post_content);[/php]

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: Code bởi http://blog.qdk.vn

- Nhấn trích dẫn bài viết để copy code chuẩn

- Các bạn tôn trọng người share chút nhé. Thanks

Back to posts
Comments:
[2018-07-08 15:40:03] ParmaSmotowah:

online casino real money <a href="https://onlinecasinogameslime.com/">online casinos</a> best online casinos <a href="https://onlinecasinogameslime.com/">best online casinos</a> | online casino slots free online casino slots

[2018-07-08 15:28:47] MUBBAIMIFRILYMN:

online gambling <a href="https://onlinecasinoslotster.com/">casino play</a> casino games <a href="https://onlinecasinoslotster.com/">online casinos for us players</a> | https://onlinecasinoslotster.com/ - online casino https://onlinecasinoslotster.com/ - casino slots

[2018-07-08 15:28:46] gueddyrubre:

gsn casino <a href="https://onlinecasinoslotsmaxx.com/">best online casinos</a> gsn casino games <a href="https://onlinecasinoslotsmaxx.com/">online gambling</a> | https://onlinecasinoslotsmaxx.com/ - casino blackjack https://onlinecasinoslotsmaxx.com/ - bovada casino

[2018-07-08 15:27:37] unlislity:

casino blackjack <a href="https://bestonlinecasinosky.com/">online gambling casino</a> real money casino <a href="https://bestonlinecasinosky.com/">online casino slots</a> | https://bestonlinecasinosky.com/ - gsn casino games https://bestonlinecasinosky.com/ - slots for real money

[2018-07-08 15:27:31] Uncedaanoda:

gsn casino games <a href="https://casinogamesamericanno.com/">real money casino</a> gsn casino <a href="https://casinogamesamericanno.com/">casino blackjack</a> | https://casinogamesamericanno.com/ - betfair online casino https://casinogamesamericanno.com/ - casino slots

[2018-07-08 15:27:26] immorouck:

online casinos <a href="https://casinoonlinegames2018.com/">casino online</a> real money casino <a href="https://casinoonlinegames2018.com/">online casino gambling</a> | https://casinoonlinegames2018.com/ - best online casinos https://casinoonlinegames2018.com/ - casino online

[2018-07-08 15:15:24] GeceSeigoreulge:

casino play <a href="https://casinogames24play.com/">play casino</a> online casinos <a href="https://casinogames24play.com/">free online casino slots</a> | https://casinogames24play.com/ - online casinos for us players https://casinogames24play.com/ - online casino gambling

[2018-07-08 15:12:01] Optiorpioniz:

online slots free <a href="https://casinoslotslime.com/">online slots free</a> online slots <a href="https://casinoslotslime.com/">free online slots vegas world</a> | https://casinoslotslime.com/ - slot machines https://casinoslotslime.com/ - turning stone online slots

[2018-07-08 15:11:05] poenueirresse:

slots online <a href="https://casinoslotsus.com/">slot games</a> turning stone online slots <a href="https://casinoslotsus.com/">slots online</a> | online slot games online slots free

[2018-07-08 15:10:48] KellaPlusleva:

bovada casino <a href="https://casinogames888bit.com/">mgm online casino</a> online casino slots <a href="https://casinogames888bit.com/">online casinos for us players</a> | online gambling slots for real money


Post a comment

Trang chủ Blog giải trí
Trang chủ Trang trước
Bộ đếm: 3 | 145 | 1086272 |