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-01 14:05:08] Agufyjatry:

online casinos for us players <a href="https://onlinecasino2018.us.org/">casino bonus</a> online casinos | https://onlinecasino2018.us.org/ - gsn casino

[2018-07-01 11:20:42] Aaqgijatry:

online casino slots <a href="https://onlinecasino2018.us.org/">online gambling</a> casino blackjack | https://onlinecasino2018.us.org/ - mgm online casino

[2018-07-01 08:39:12] Aatinjatry:

online casino real money <a href="https://onlinecasino2018.us.org/">online casino games</a> best online casinos | https://onlinecasino2018.us.org/ - betfair online casino

[2018-07-01 06:02:08] Ajbppjatry:

mgm online casino <a href="https://onlinecasino2018.us.org/">slots for real money</a> casino bonus | https://onlinecasino2018.us.org/ - online casino

[2018-07-01 03:38:07] Afjjvjatry:

usa online casino <a href="https://onlinecasino2018.us.org/">online casino gambling</a> best online casino | https://onlinecasino2018.us.org/ - best online casinos

[2018-07-01 01:18:08] Aqrlijatry:

betfair online casino <a href="https://onlinecasino2018.us.org/">casino online</a> online casino slots | https://onlinecasino2018.us.org/ - online casinos

[2018-07-01 00:30:35] Anokcjatry:

play casino <a href="https://onlinecasinoplay.us.org/">casino play</a> online casinos for us players | https://onlinecasinoplay.us.org/ - online casino gambling

[2018-06-30 23:00:55] Aasdljatry:

online casino games <a href="https://onlinecasino2018.us.org/">casino slots</a> betfair online casino | https://onlinecasino2018.us.org/ - gsn casino games

[2018-06-30 22:19:06] Altjhjatry:

real money casino <a href="https://onlinecasinoplay.us.org/">betfair online casino</a> online gambling | https://onlinecasinoplay.us.org/ - gsn casino games

[2018-06-30 20:52:46] Aufpejatry:

slots for real money <a href="https://onlinecasino2018.us.org/">usa online casino</a> casino online | https://onlinecasino2018.us.org/ - usa online casino


Post a comment

Trang chủ Blog giải trí
Trang chủ Trang trước
Bộ đếm: 50 | 76 | 1087732 |