Write PHP script to achieve WordPress comments Paging function _php Skills

Source: Internet
Author: User
Tags comments php script

Method description

First look at the methods that might be used. Open File wp-includes/link-template.php you will find WordPress 2.7 more than 4 comments page-specific methods:

    • Get_comments_pagenum_link
    • $pagenum: Page number
    • $max _page: Maximum pages

Returns the link address. In this application will not be used, but we should know that there is this method, will facilitate you to make plug-ins.

Next_comments_link

$label: The typeface displayed, by default, is»newer Comments. But I always think this is a bug, should be newer comments»?

$max _page: Maximum pages

Displays a link to the next page.

Previous_comments_link
$label: The typeface displayed, by default, is «older Comments.
Displays a link to the previous page.

Paginate_comments_links
$args: String parameter Group
-Base: Page URL
-Format: I don't know what it is. Who can tell me?
-Total: Maximum pages
-present: Current page number
-Echo: is displayed, default is display. Returns string HTML when not displayed.
-Add_fragment:hash, default is #comments. In order to be unified, convenient to understand, also lest revise, suggest to produce the topic when the comment part uses id= "Comments"
HTML that displays comment pagination or returns comment pagination.

Example description

1. Previous page and next page
This is the simplest and most commonly mentioned, and the effect is no different from the page.

<div id= "Commentnavi" >
 <span class= "Floatleft" ><?php previous_comments_link (' «older Comments '))?></span>
 <span class= "Floatright" ><?php next_comments_link (__ (' newer comments» ') )?></span>
 <div class= "fixed" ></div>
</div>

2. Navigation page-pagination
the most transparent to the visitors, the most convenient use, the effect of the use of Wp-pagenavi paging is similar.

<div id= "Commentnavi" >
 <?php paginate_comments_links ();?>
</div>

Take a look at the previous two examples, as if the functionality we're looking for is achievable. But don't you think it's a little weird? If you choose not to display a comment page in the background, or if the number of comments is not sufficient, do you want to display the id= "Commentnavi" box? No! We have to change this procedure.
You can get the comment paging option in the background by calling method Get_option (' page_comments ').
We can also call method Paginate_comments_links (' echo=0 ') to know if the comments are adequate and need to be paged.

3. Previous page and next page (improved)

<?php
 ///If the user chooses to display comments in the background page if
 (get_option (' page_comments ')) {
 //Get comment pagination HTML
 $comment _pages = Paginate_comments_links (' echo=0 ');
 If the comment pagination HTML is not empty, display the link to the previous page and the next page if
 ($comment _pages) {
?>
 <div id= "Commentnavi" >
 <span class= "Floatleft" ><?php previous_comments_link (__ (' «older comments '))?></span>
 <span class = "Floatright" ><?php next_comments_link (__ (' newer comments» '))?></span> <div class=
 "fixed" ></div>
 </div>
<?php
 }
 }
?>

4. Navigation paging (improved)

<?php
 ///If the user chooses to display comments in the background page if
 (get_option (' page_comments ')) {
 //Get comment pagination HTML
 $comment _pages = Paginate_comments_links (' echo=0 ');
 If the HTML for the comment pagination is not empty, display navigation pagination
 if ($comment _pages) {
?>
 <div id= "Commentnavi" >
 <?php echo $ Comment_pages?>
 </div>
<?php
 }
 }
?>

For the latest comment paging feature, I have a bit of my own opinion, also casually say it.
Comment Pagination is not perfect. If an outside link jumps to the http://www.example.com/#5630, but the comment number 5630 is not on the first page, then it is impossible to locate the comment, and the brother who jumps over it will feel rather uncomfortable.
In addition, WordPress's comments page is not AJAX application (for browser compatibility considerations, it is unlikely to be later), so the paging will be quite time-consuming. So I suggest that you set a larger number of comments per page, even if you are paging. 30? 50? 100? Vary by Bo.

Well, maybe you already know how to use it? CSS is not much to say, tastes well, this is not easy to say.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.