• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Hướng dẫn phân trang bài viết wordpress

Admin

Well-Known Member
Staff member
Administrator
Phân bằng bằng file wp-includes/query.php
đầu tiên bạn mở wp-includes/query.php:
Tìm đoạn
PHP:
/**
* Set up global post data.
*
* @since 1.5.0
*
* @param object $post Post data.
* @uses do_action_ref_array() Calls 'the_post'
* @return bool True when finished.
*/
function setup_postdata( $post ) {
    global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;

    $id = (int) $post->ID;

    $authordata = get_userdata($post->post_author);

    $currentday = mysql2date('d.m.y', $post->post_date, false);
    $currentmonth = mysql2date('m', $post->post_date, false);
    $numpages = 1;
    $multipage = 0;
    $page = get_query_var('page');
    if ( ! $page )
        $page = 1;
    if ( is_single() || is_page() || is_feed() )
        $more = 1;
    $content = $post->post_content;
    if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
        if ( $page > 1 )
            $more = 1;
        $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
        $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
        $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
        // Ignore nextpage at the beginning of the content.
        if ( 0 === strpos( $content, '<!--nextpage-->' ) )
            $content = substr( $content, 15 );
        $pages = explode('<!--nextpage-->', $content);
        $numpages = count($pages);
        if ( $numpages > 1 )
            $multipage = 1;
    } else {
        $pages = array( $post->post_content );
    }

    do_action_ref_array('the_post', array(&$post));

    return true;
}

Thay đoạn đó bằng
PHP:
/**
* Set up global post data.
*
* @since 1.5.0
*
* @param object $post Post data.
* @uses do_action_ref_array() Calls 'the_post'
* @return bool True when finished.
*/
function setup_postdata($post) {
    global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;

    $id = (int) $post->ID;

    $authordata = get_userdata($post->post_author);

    $currentday = mysql2date('d.m.y', $post->post_date, false);
    $currentmonth = mysql2date('m', $post->post_date, false);
    $numpages = 1;
    $page = get_query_var('page');
    if ( !$page )
        $page = 1;
    if ( is_single() || is_page() || is_feed() )
        $more = 1;
    $content = $post->post_content;
    if ( strpos( $content, '<!--nextpage-->' ) ) {
        if ( $page > 1 )
            $more = 1;
        $multipage = 1;
        $pages = explode('<!--nextpage-->', $content);
        $numpages = count($pages);
    } else {
        ini_set('memory_limit', '500M');
        $max_lex = 5000;
        $string = $content;
        $strrpos = mb_strrpos($content," ");
        $current = 0;
        $pagesPost = ceil($strrpos/$max_lex);
        $pagePost = 1;
        if ($strrpos > $max_lex) {
            $content = '';
            while ($current < $strrpos) {
            $stringtt = mb_substr($string, $current, $max_lex);
            $numStr = mb_strrpos($stringtt," ");
            if($pagePost<$pagesPost)  $content .=  $stringtt."<!--nextpage-->";  else $content .= $stringtt;
            $current = $numStr + $current;
            $pagePost++;
            }
        } else {
        $content = $string;
    }
        if ( $page > 1 ) $more = 1;
        $multipage = 1;
        $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
        $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
        $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
        $pages = explode('<!--nextpage-->', $content);
        $numpages = count($pages);
        /*
        $pages = array( $post->post_content );
        $multipage = 0;
        */
    }

    do_action_ref_array('the_post', array(&$post));

    return true;
}
Trong đó:

$max_lex = 5000; //Số ký tự bạn muốn trong một trang

Cho code này vào chỗ bạn muốn để phân trang :D
PHP:
<?php link_pages('<p>Trang:</strong> ', '</p>', 'number'); ?>
Về css thì bạn tự chỉnh nhé :)

Phần tiếp theo là mình hướng dẫn các bạn rút gọn cho đẹp :D
* Phần này làm theo của QDK nhé :)
Các bạn mở file funtions.php của theme
Thêm đoạn này vào
PHP:
<?php
function wapvn_link_pages() {
    global $page, $numpages, $multipage;
    if ( $multipage ) {
        $out = array();
        if($numpages > 5) {
            if($page > 1) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($numpages - 1) . '">&lt;&lt;</a> ';
            if($page > 3) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page(1) . '">1</a> ';
            if($page > 4) $out[] = '<span style="font-weight: bold;">&larr;</span> ';
        }
        for ($i = 1; $i <= $numpages; $i++) {
            if (!($i >= ($page + 3) || $i <= ($page - 3)) || $numpages <= 5) {
                if ($i == $page) {
                    $out[] = '<span class="currentpage"><b>' . $i . '</b></span>';
                } else {
                    $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($i) . '">' . $i . '</a>';
                }
            }
        }
        if ($numpages > 5) {
            if ($numpages > ($page + 3)) $out[] = '<span style="font-weight: bold;">&rarr;</span>';
            if ($numpages > ($page + 2)) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($numpages) . '">' . $numpages . '</a>';
            if ($numpages > $page) $out[] = '<a class="pagenav" href="' . wapvn_get_link_page($page + 1) . '">&gt;&gt;</a>';
        }
        echo '<div class="topmenu">' . implode(' ', $out) . '</div>';
    }
}
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);
}
?>

Sau đó dùng
PHP:
<?php wapvn_link_pages(); ?>
Thay vào chỗ mà bạn để
PHP:
<?php link_pages('<p>Trang:</strong> ', '</p>', 'number'); ?>

Tiếp là trang trí css
Mở Style.css
Thêm:
Code:
.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;
}

Màu sắc css thì các bạn có thể tự chỉnh theo ý nhé
Demo: http://anhvang.biz/truyen-teen-em-khong-vao-dia-nguc-thi-ai-vao-full.html

Lưu ý nhé: Khi bạn nâng cấp lên ver WP thì phải làm lại nhé :D
 
Em post đủ 20 top có ích thì pm anh, không comment lung tung nhé
 

Facebook Comments

New posts New threads New resources

Back
Top