Implementation of the previous page and next page on a single WordPress page

Source: Internet
Author: User
Tags foreach explode prev sort

The page has a hierarchical function. The sub-pages sorted by menu order have links of the previous and next pages, for example:

Themes (parent page)
---- Z (sublevel page 1)
---- ZBorder (sublevel page 2)
---- ZSofa (sublevel page 3)

If the current page is zBorder, the previous link is zBorder, and the next link is zSofa.

Put the following function code into functions. php (note: The function is handwritten and may not be simplified enough)

/**
* Get subpage previous/next page link by zwwooooo
*/
Function subpage_nav_link ($ prevText = '', $ nextText = ''){
Global $ post;
If (! $ Post-> post_parent) return null; // if it is not a child page, Null is returned.
$ Args = array (
'Sort _ order' => 'asc ',
'Sort _ column' => 'menu _ order ',
'Child _ of '=> $ post-> post_parent,
'Post _ type' => 'Page'
);
$ Pages = get_pages ($ args );
$ Num = count ($ pages );
$ I = 0;
$ Index =-1;
Foreach ($ pages as $ page ){
If ($ page-> ID = $ post-> ID ){
$ Index = $ I;
Break;
        }
++ $ I;
    }
If ($ I = 0 ){
$ Prev = '';
$ Next = $ pages [$ index + 1];
} Elseif ($ I ==$ num-1 ){
$ Prev = $ pages [$ index-1];
$ Next = '';
} Else {
$ Prev = $ pages [$ index-1];
$ Next = $ pages [$ index + 1];
    }
If ($ prev ){
If ($ prevText ){
If (substr_count ($ prevText, '% title')> 0 ){
$ Explode = explode ('% title', $ prevText );
$ PrevText = $ explode [0]. get_the_title ($ prev-> ID). $ explode [1];
            }
} Else {
$ PrevText = get_the_title ($ prev-> ID );
        }
$ Prevlink = '<a class = "previous-page-link" href = "'. get_page_link ($ prev-> ID ). '"> '. $ prevText. '</a> ';
    }
If ($ next ){
If ($ nextText ){
If (substr_count ($ nextText, '% title')> 0 ){
$ Explode = explode ('% title', $ nextText );
$ NextText = $ explode [0]. get_the_title ($ next-> ID). $ explode [1];
            }
} Else {
$ NextText = get_the_title ($ next-> ID );
        }
$ Nextlink = '<a class = "next-page-link" href = "'. get_page_link ($ next-> ID ). '"> '. $ nextText. '</a> ';
    }
Return array ($ prevlink, $ nextlink );
}

[Function]

Subpage_nav_link ($ prevText, $ nextText)

[Parameters]

$ PrevText: the link text of the previous article. If it is null, the page title is used by default.
$ NextText: the link text of the next article. If it is null, the page title is used by default;

For example, the general topic is to insert the call code in the loop of page. php (if you don't know it, just in the_content (); below).

<? Php
If (function_exists ('subpage _ nav_link ')){
If ($ subpage_nav_link = subpage_nav_link ()){
Echo $ subpage_nav_link [0]; // link of the previous article (page)
Echo $ subpage_nav_link [1]; // Next (page) link
    }
}
?>

Note: if (! $ Subpage_nav_link [0]) to determine whether there is any previous article. Similarly, if (! $ Subpage_nav_link [1]) to determine whether there is any next article.

PS: $ prevText and $ nextText also support character combinations, such as subpage_nav_link ('oo % title xx, the link to the previous article will be changed to "oo page name xx"


Another practical article: implement wordpress article page calling in the same category/next article

The function code provided by wordpress to display the previous and next articles is called in the order of release. The wordpress novel template made a few days ago, because the topic template wpnovel of blog's first wordpress novel website is added to each category, it is inappropriate to display the call sequence of the previous and next articles, let the previous and next articles under the same category show on the article page. wordpress is powerful and can always satisfy users' ideas and find relevant function code through searching.

Code directly called by default

<? Php previus_post_link ('previous: % link')?>
<? Php next_post_link ('next article: % link')?>

When the article is in the first or last article, it will be blank, but it can be filled by adding judgment

<? Php if (get_previus_post () {previus_post_link ('previous: % link') ;}else {echo "is the last article" ;}?>
<? Php if (get_next_post () {next_post_link ('next: % link');} else {echo "is the latest article" ;}?>

Although the articles under the same category are displayed in the test, the first and last articles do not show the corresponding prompts "already the last article" and "already the last article ". You only need to specify the document Category ID in the get_previus_post () function to make the code fully valid.

The complete code is as follows:

<? Php
$ Categories = get_the_category ();
$ CategoryIDS = array ();
Foreach ($ categories as $ category ){
Array_push ($ categoryIDS, $ category-> term_id );
        }
$ CategoryIDS = implode (",", $ categoryIDS );
?>
<? Php if (get_previus_post ($ categoryIDS) {previus_post_link ('previous: % link', '% title', true);} else {echo "is the last article" ;}?>
<? Php if (get_next_post ($ categoryIDS) {next_post_link ('previous: % link', '% title', true);} else {echo "is the latest article" ;}?>

Open the article page single. php in the topic directory, add code to the location to be displayed, and save the file.

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.