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 23:09:26] poenueirresse:

vegas casino slots <a href="https://casinoslotsus.com/">online casino slots</a> best online slots <a href="https://casinoslotsus.com/">free online slots vegas world</a> | https://casinoslotsus.com/ - casino slots https://casinoslotsus.com/ - casino slots

[2018-07-08 23:08:55] KellaPlusleva:

play casino <a href="https://casinogames888bit.com/">betfair online casino</a> betfair online casino <a href="https://casinogames888bit.com/">online casino games</a> | https://casinogames888bit.com/ - online casinos https://casinogames888bit.com/ - online casino

[2018-07-08 23:06:49] Aqhbijatry:

online casinos <a href="https://onlinecasinoplay.us.org/">casino play</a> online gambling casino <a href="https://onlinecasinoplay.us.org/">best online casinos</a> - https://onlinecasinoplay.us.org/ - gsn casino - https://onlinecasinoplay.us.org/ - casino online

[2018-07-08 22:53:38] Bymnemolley:

online slots <a href="https://ieonlineslots.com/">play slots online</a> online slots <a href="https://ieonlineslots.com/">vegas world free games online slots</a> | free online slots no download slot games

[2018-07-08 22:53:35] DolaMiliaBiz:

online casino slots <a href="https://ieslotsonline.com/">play slots online</a> free online slots <a href="https://ieslotsonline.com/">slots games</a> | vegas slots online free games online slots

[2018-07-08 22:51:20] tierimaprease:

online casino <a href="https://casinoonlineslotsy.com/">best online casino</a> casino real money <a href="https://casinoonlineslotsy.com/">slots for real money</a> | casino online online gambling casino

[2018-07-08 22:47:36] ParmaSmotowah:

online casinos for us players <a href="https://onlinecasinogameslime.com/">best online casinos</a> online casino games <a href="https://onlinecasinogameslime.com/">online casino bonus</a> | betfair online casino online casino

[2018-07-08 22:47:21] Audinstitonetty:

gsn casino <a href="https://onlinecasinoslotswmw.com/">best online casinos</a> play casino <a href="https://onlinecasinoslotswmw.com/">online casino slots</a> | bovada casino casino games

[2018-07-08 22:47:18] Neawelade:

online casino gambling <a href="https://onlinecasinoslotsplay13.com/">best online casino</a> casino blackjack <a href="https://onlinecasinoslotsplay13.com/">slots for real money</a> | real money casino free online casino slots

[2018-07-08 22:44:59] Soneawanneniats:

bovada casino <a href="https://bestonlinecasinoppul.com/">online casino gambling</a> online casino slots <a href="https://bestonlinecasinoppul.com/">casino real money</a> | casino bonus bovada casino


Post a comment

Trang chủ Blog giải trí
Trang chủ Trang trước
Bộ đếm: 11 | 241 | 1087126 |