Home >Web Front-end >JS Tutorial >How to implement the previous page and next page of a WordPress single page [with code]_javascript skills

How to implement the previous page and next page of a WordPress single page [with code]_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:11:051761browse

WordPress article pages have functions to implement the previous and next articles. However, we want to implement the previous and next page functions in a single page page.php. The previous_post_link() and next_post_link() functions are not yet available. It completely met my needs, so I wrote the function myself to implement it.
The page has a hierarchical function. The requirement is that there are previous and next links between sub-pages sorted by menu order, such as:

Themes (parent page)
---- zBench (child page 1)
---- zBorder (child page 2)
---- zSofa (child page 3)

If the current page is zBorder, then the previous link must be zBench and the next link must be zSofa.

把下面函数代码放入 functions.php(注:函数随手写的,可能不够精简)

/**
* get subpage previous/next page link by zwwooooo
*/
function subpage_nav_link($prevText='', $nextText='') {
global $post;
if ( !$post->post_parent ) return null; //如果不是子页面返回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)

[Parameter]

$prevText: Link text for the previous article. If empty, the default is the page title
$nextText: Link text for the next article. When empty, it defaults to the page title;

For example: the general theme is to insert the calling code in the loop loop of page.php (I don’t know, just below the_content();)

Copy code The code is as follows:

df1efdc1cb6a4c4c54bf61105814557e

Note: You can use if (!$subpage_nav_link[0]) to determine whether there is a previous article, and similarly if (!$subpage_nav_link[1]) to determine whether there is a next article.

PS: $prevText and $nextText also support character combinations, such as subpage_nav_link('oo %title xx', '') In this case, the previous article link article will become "oo page name xx"


Another practical article: Implementing the wordpress article page to call the previous/next article in the same category

The function code provided by wordpress to display the previous and next articles is called according to the order of publication. The wordpress novel template I made a few days ago, because each category is used to add a novel "Blog Bar's first wordpress novel" Website theme template wpnovel", if it is not appropriate to use the order of calling the previous and next articles, it is the right way to let the article page display the previous and next articles under the same category. WordPress is powerful and can always satisfy the user's ideas. , the relevant function code was found through search.

Code called directly by default

657decae7fcc18f654b1013ff20a967f
4290abfbfdf0dd37f4f3a177a5497bb6

When the article is the first or last article, gaps will be displayed, but the gaps can be filled by adding judgment

dba8d5dc6b4d724c6c489741b901395c
f357eb9c8374186f4abc8ebe0a67d6eb

After testing, although articles under the same category are displayed, the first article and the last article will not display the corresponding prompt messages "Already the last article" and "Already the last article". As long as you specify the category ID of the article in the get_previous_post() function, the code will be fully effective.

Here is the complete code:

Copy code The code is as follows:

978f6d1a0caca4d945459c0f9731979aterm_id);
}
$categoryIDS = implode(",", $categoryIDS);
?>
bb3746b006bdc143861d9cb1193df467
b293ade4e8874288ac4d73691ffea8be

Open the article page single.php in the theme directory, add the code where you want it to be displayed, and save the file.

The above method of implementing the previous page and next page of a WordPress single page [with code] is all the content shared by the editor. I hope it can give you a reference, and I also hope you will support Script Home.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn