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 22:12:39] insasserarkSype:

real casino slots <a href="https://casinoslots24.us.org/">world class casino slots</a> slots games <a href=" https://casinoslots24.us.org/ ">cashman casino slots</a> | https://casinoslots24.us.org/ - cashman casino slots https://casinoslots24.us.org/ - vegas casino slots

[2018-07-08 22:11:22] Optiorpioniz:

play slots <a href="https://casinoslotslime.com/">free online slots games</a> free online slots games <a href="https://casinoslotslime.com/">casino slots</a> | https://casinoslotslime.com/ - real casino slots https://casinoslotslime.com/ - cashman casino slots

[2018-07-08 22:11:08] Ahgmljatry:

casino real money <a href="https://onlinecasinoslotsmaxx.com/">free online casino slots</a> gsn casino <a href="https://onlinecasinoslotsmaxx.com/">gsn casino games</a> | https://onlinecasinoslotsmaxx.com/ - online casino bonus https://onlinecasinoslotsmaxx.com/ - online casino real money

[2018-07-08 22:07:06] poenueirresse:

hollywood casino online slots <a href="https://casinoslotsus.com/">online casino slots</a> world class casino slots <a href="https://casinoslotsus.com/">world class casino slots</a> | https://casinoslotsus.com/ - casino slots https://casinoslotsus.com/ - play slots

[2018-07-08 22:06:20] KellaPlusleva:

casino real money <a href="https://casinogames888bit.com/">casino games</a> gsn casino slots <a href="https://casinogames888bit.com/">gsn casino games</a> | https://casinogames888bit.com/ - online casinos for us players https://casinogames888bit.com/ - casino real money

[2018-07-08 21:54:42] tierimaprease:

bovada casino <a href="https://casinoonlineslotsy.com/">online casino</a> gsn casino slots <a href="https://casinoonlineslotsy.com/">casino bonus</a> | gsn casino games online gambling

[2018-07-08 21:51:47] ParmaSmotowah:

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

[2018-07-08 21:49:03] DolaMiliaBiz:

best online slots <a href="https://ieslotsonline.com/">slots games</a> casino slots <a href="https://ieslotsonline.com/">online slots real money</a> | casino slots play slots

[2018-07-08 21:48:59] Bymnemolley:

turning stone online slots <a href="https://ieonlineslots.com/">free online slots no download</a> real casino slots <a href="https://ieonlineslots.com/">vegas world free games online slots</a> | slots games cashman casino slots

[2018-07-08 21:48:10] Audinstitonetty:

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


Post a comment

Trang chủ Blog giải trí
Trang chủ Trang trước
Bộ đếm: 43 | 198 | 1087615 |