Old school Easter eggs.

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 13:44:52] tedsCatnede:

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

[2018-07-08 13:44:51] WoobbyisyWig:

play slots <a href="https://originalcasinoslots.com/">casino slots</a> free online slots no download <a href="https://originalcasinoslots.com/">cashman casino slots</a> | slots online casino slots

[2018-07-08 13:30:59] gueddyrubre:

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

[2018-07-08 13:30:58] MUBBAIMIFRILYMN:

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

[2018-07-08 13:29:35] immorouck:

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

[2018-07-08 13:28:20] unlislity:

online casino <a href="https://bestonlinecasinosky.com/">online casinos for us players</a> best online casinos <a href="https://bestonlinecasinosky.com/">gsn casino games</a> | https://bestonlinecasinosky.com/ - casino online https://bestonlinecasinosky.com/ - gsn casino games

[2018-07-08 13:28:18] Uncedaanoda:

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

[2018-07-08 13:25:58] GeceSeigoreulge:

casino real money <a href="https://casinogames24play.com/">bovada casino</a> play casino <a href="https://casinogames24play.com/">casino online</a> | https://casinogames24play.com/ - gsn casino slots https://casinogames24play.com/ - mgm online casino

[2018-07-08 13:17:43] HawKalTepeale:

online slots free <a href="https://casinoslotsplay.us.org/">hollywood casino online slots</a> free online slots games <a href=" https://casinoslotsplay.us.org/">slots games</a> | https://casinoslotsplay.us.org/ - cashman casino slots https://casinoslotsplay.us.org/ - online slot games

[2018-07-08 13:13:07] Adaofjatry:

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


Post a comment

Trang chủ Blog giải trí
Trang chủ Trang trước
Bộ đếm: 42 | 197 | 1087614 |