Implementation of the next page on a single WordPress page [Code], wordpress Previous Page

Source: Internet
Author: User

Implementation of the next page on a single WordPress page [Code], wordpress Previous Page

The WordPress article page provides functions for the previous article, but we want to go to the single page. php implements the next page of the previous page. The previus_post_link () and next_post_link () functions cannot fully meet my needs, so I need to write the function myself.
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 in functions.php (Note: The function is written by hand, it may not be simplified)

/ **
* 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 subpage, return Null
$ 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 ).

Copy codeThe Code is as follows:
<? 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:

Copy codeThe 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.

The implementation method [with code] for the previous page of the WordPress single page above is all the content shared by the editor. I hope to give you a reference and support for the customer's house.


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.