WordPress single page Next page implementation method "code" _javascript tips

Source: Internet
Author: User
Tags explode

WordPress article page has the implementation of the next article on the function function, but we want to implement the next page in a single page page.php, Previous_post_link () and Next_post_link () function is not fully meet my needs, So write your own function to achieve it.
The page has a rating function, the requirement is to sort by menu order of the child page between the previous, next link, 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 is zbench, and the next link is zsofa.

Put the following function code into functions.php (Note: The function is written, may not be concise)/** * Get subpage previous/next page link by zwwooooo/function subpage_nav_l
Ink ($prevText = ', $nextText = ') {global $post; if (! $post->post_parent) return null;//If not a subpage returns null $args = Array ( ' Sort_order ' => ' ASC ', ' sort_column ' => ' menu_order ', ' child_of ' => $post->post_parent, ' post_type ' => ' PA
GE ');
$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: Link text for previous post, default is page title when blank
$nextText: For the next article link text, is empty when the default is the page title;

For example: The general theme is inside the loop loop of the page.php (The_content (); below) Insert the calling code

Copy Code code as follows:

<?php
if (function_exists (' Subpage_nav_link ')) {
if ($subpage _nav_link = Subpage_nav_link ()) {
echo $subpage _nav_link[0]; Previous (page) Link
echo $subpage _nav_link[1]; Next (page) Link
}
}
?>

Note: You can use if (! $subpage _nav_link[0]) to determine if there is no previous article, the same if (! $subpage _nav_link[1]) to determine if there is no next.

PS: $prevText and $nextText also support character combinations, such as Subpage_nav_link (' oo%title xx '), the previous article linked to the article will become "OO page name xx"


Another practical article: the implementation of WordPress article page call the same category/next article

WordPress provides the display of the previous, the next article of the function code is called in order to publish, the previous few days to do the WordPress novel template, because of the use of each category to add a novel "blog Bar the first WordPress novel website theme template Wpnovel", If you use such an upper and lower article call order display is not appropriate, so that the article page to show the same category of the previous, the next article is the right way, WordPress is powerful, always meet the user's ideas, through the search to find the relevant function code.

Code that is called directly by default

<?php Previous_post_link (' previous:%link ')?>
<?php Next_post_link (' Next:%link ')?>

When the article is in the first or last chapter, the blank is displayed, but the blank can be filled by increasing the judgment

<?php if (Get_previous_post ()) {Previous_post_link (' previous:%link ');} else {echo "is the last article";}?>
<?php if (Get_next_post ()) {Next_post_link (' Next:%link ');} else {echo is already the latest article;}?>

While testing shows the same category of articles, but the first article and the end of the article will not display the corresponding message "is the last article" and "is the last article." Simply specifying the category ID of the article in the Get_previous_post () function makes the code completely valid.

Here's the complete code:

Copy Code code 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_previous_post ($categoryIDS)) {Previous_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 already the latest article;}?>

Open the article page under the topic directory single.php, add the code to the location you want to display, and save the file.

The above WordPress single page on the next page of the implementation of the method "with code" is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.