admin管理员组

文章数量:1291503

Does anyone know if it's possible to change wp_link_pages, I want to change the order of the pages Example : i have an Post consisting of 5 pages i want when you click on "Next page", it takes you to a random page of the five pages that we have in the article. without repeat

Any ideas/help greatly appreciated, S.

Does anyone know if it's possible to change wp_link_pages, I want to change the order of the pages Example : i have an Post consisting of 5 pages i want when you click on "Next page", it takes you to a random page of the five pages that we have in the article. without repeat

Any ideas/help greatly appreciated, S.

Share Improve this question asked May 15, 2021 at 0:45 Said ErraoudySaid Erraoudy 634 bronze badges 3
  • What do you mean by, you have a post containing 5 pages? how can a post contain pages exactly? – Buttered_Toast Commented May 24, 2021 at 9:49
  • 1 @Buttered_Toast by adding <!--nextpage--> 5 time in a single post you get one post with 5 pages right ? – Said Erraoudy Commented May 24, 2021 at 10:37
  • Would it be possible to show an example? maybe add your code that you have done so far, even provide a link to what you are working on. If it's local, provide us with a way to replicate what you currently have – Buttered_Toast Commented May 24, 2021 at 10:47
Add a comment  | 

1 Answer 1

Reset to default 3

Changing the wp_link_pages() function is an option, but randomizing either the page numbers or page links may be detrimental to the user's experience as he's expecting a normal page link structure while noticing random page numbers, e.g. in the post's URL.

However, there exists a content_pagination filter that lets you alter the post's $pages array. Simply shuffling this array would suffice, however, this does neither guarantee a static page order nor does it exclude the possibility of page repetition, as the shuffle is executed on each page load.

You could solve both these problems by using array_multisort() in combination with mt_srand(), as explained in this answer, and seed the random number generator with e.g. the post's ID—but then, every user is served the same (pseudo-random) page order, and one could argue what's the use of shuffling the pages at all.

You could mix in the user ID (if any) in the seed, e.g. by multiplying the post ID with the user ID, to increase the chances of giving each registered user their "own" static page order that varies across posts. In the end, it really depends on what you want, and how far you are willing to go with it.

Code example (seeded with post ID):

function shuffle_pages( array $pages, WP_Post $post ): array {
    mt_srand( $post->ID );
    $order = array_map( function ( $val ) { return mt_rand(); }, range( 1, count( $pages ) ) );
    array_multisort( $order, $pages );
    return $pages;
}
add_filter( 'content_pagination', 'shuffle_pages', 10, 2 );

本文标签: paginationCustom wplinkpages for paginated postsNext page randomly