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();)
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
When the article is the first or last article, gaps will be displayed, but the gaps can be filled by adding judgment
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:
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.