QUICK TIP - WordPress Break Your post to 2 pages with pagination-PHP

- 1. QUICK TIP - Most Useful .htaccess Tricks for WordPress
- 2. QUICK TIP - How to Join and Earn From Brave Ads
WordPress Break Your post to 2 pages with pagination-PHP will support you to custom pagination for your posts. This tutorial will help you to do the task. Your posts will be break into 2 pages depend on your post design.
For example, this tutorial will break the post into 2 page-links. One will show you the Questions and other page-link will reveal the answers.
What is Pagination?
Pagination allows your user to page back and forth through multiple pages of content.
WordPress can use pagination when:
- Viewing lists of posts when more posts exist than can fit on one page, or
- Breaking up longer posts by manually by using the following tag.
1
<!--nextpage-->
Using Pagination to Navigate Post Lists Using Pagination to Navigate Post Lists
The most common use for pagination in WordPress sites is to break up long lists of posts into separate pages. Whether you’re viewing a category, archive, or default index page for a blog or site, WordPress only shows 10 posts per page by default. Users can change the number of posts that appear on each page on the Reading screen: Admin > Settings > Reading.
How to the task
The main task of the tutorial will design the pagination your post like image below.
So if you have any of those files in your theme please look at theme first.
Before we go to next steps, we should know about function.php. If you already know it, please ignore this section and go to next step.
What is : function.php
In WordPress, functions.php or the theme functions file is a template included in WordPress themes. It acts like a plugin for your WordPress site that’s automatically activated with your current theme. The functions.php file uses PHP code to add features or change default features on a WordPress site.
The functions.php file automatically loads when you install and activate a theme on your WordPress site.
Editing the functions.php file using custom codes allows you to add post types, taxonomies, shortcodes, and more to improve your website.
How to implement the code for custom pagination
Put this function in you functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
ahbusinesstechnology_questions_answer_wp_link_pages( $args = '' ) { global $page, $numpages, $multipage, $more; $defaults = array( 'before' => '<p>' . __( 'Pages:' ), 'after' => '</p>', 'link_before' => '', 'link_after' => '', 'next_or_number' => 'number', 'separator' => ' ', 'nextpagelink' => __( 'Next page' ), 'previouspagelink' => __( 'Previous page' ), 'pagelink' => '%', 'echo' => 1 ); $params = wp_parse_args( $args, $defaults ); /** * Filters the arguments used in retrieving page links for paginated posts. * * @param array $params An array of arguments for page links for paginated posts. */ $r = apply_filters( 'wp_link_pages_args', $params ); $output = ''; if ( $multipage ) { if ( 'number' == $r['next_or_number'] ) { $output .= $r['before']; for ( $i = 1; $i <= $numpages; $i++ ) { if ( $i % 2 == 0) { $link = $r['link_before'] . str_replace( '%', 'Answer', $r['pagelink'] ) . $r['link_after']; } else { $link = $r['link_before'] . str_replace( '%', 'Question', $r['pagelink'] ) . $r['link_after']; } if ( $i != $page || ! $more && 1 == $page ) { $link = _wp_link_page( $i ) . $link . '</a>'; } /** * Filters the HTML output of individual page number links. * * AHBusinesstechnology.com * * @param string $link The page number HTML output. * @param int $i Page number for paginated posts' page links. */ $link = apply_filters( 'wp_link_pages_link', $link, $i ); // Use the custom links separator beginning with the second link. $output .= ( 1 === $i ) ? ' ' : $r['separator']; $output .= $link; } $output .= $r['after']; } elseif ( $more ) { $output .= $r['before']; $prev = $page - 1; if ( $prev > 0 ) { $link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['link_after'] . '</a>'; /** This filter is documented in wp-includes/post-template.php */ $output .= apply_filters( 'wp_link_pages_link', $link, $prev ); } $next = $page + 1; if ( $next <= $numpages ) { if ( $prev ) { $output .= $r['separator']; } $link = _wp_link_page( $next ) . $r['link_before'] . $r['nextpagelink'] . $r['link_after'] . '</a>'; /** This filter is documented in wp-includes/post-template.php */ $output .= apply_filters( 'wp_link_pages_link', $link, $next ); } $output .= $r['after']; } } /** * Filters the HTML output of page links for paginated posts. * @param string $output HTML output of paginated posts' page links. * @param array $args An array of arguments. */ $html = apply_filters( 'wp_link_pages', $output, $args ); if ( $r['echo'] ) { echo $html; } return $html; } |
And then go to your theme directory. Search and replace the wp_link_pages
function names with the_questions_answer_wp_link_pages
. And it’ll work as you wanted. Usually the wp_link_pages
exists in content-{template-name}.php
.
The function works on odd-even basis. It considers the odd part as ‘Question’ and the even part as ‘Answer’.
Besides, you can adjust its name and codes following your post design such as Exercises and Solutions, Theories and Practices, Pros & Cons …
And after that you do your styling. Have fun.