Tuesday, August 2, 2016

How to create Custom Pagination Permalinks in wordpress

Leave a Comment

I have an article with several pages in my wordpress blog. if for example i have the following link in my blog :

http://example.com/heartbreaking-photos any idea how can i change the link of the second page from

http://example.com/heartbreaking-photos/2 to http://example.com/heartbreaking-photos/CUSTOM-STRING

CUSTOM-STRING aimed to be a custom title inside the page

3 Answers

Answers 1

To achieve this, you will need to do 2 things:

  1. Disable the default WordPress canonical redirect - this is necessary, because WordPress will always redirect to the /2/ page when it encounters the page parameter in the URL or query args.

  2. Add a custom rewrite rule to direct your custom title to the second page of your page - this is essentially necessary to allow the link format that you want.

In terms of code, this is what you need (this is a working solution, I've just tested it locally):

// Removes the canonical redirection remove_filter( 'template_redirect', 'redirect_canonical' );  // Add custom rewrite rules add_action( 'init', 'my_add_custom_rewrite_rules' ); function my_add_custom_rewrite_rules() {     // Slug of the target page     $page_slug = 'heartbreaking-photos';      // Page number to replace     $page_num = 2;      // Title you wish to replace the page number with     $title = 'custom-string';      // Add the custom rewrite rule     add_rewrite_rule(         '^' . $page_slug . '/' . $title . '/?$',         'index.php?pagename=' . $page_slug . '&page=' . $page_num, 'top'     ); } 

There are three things you might want to configure or change here:

  1. $page_slug - this is the slug of your page, in your case this is heartbreaking-photos
  2. $page_num - the number of your pagination page, in your case this is 2
  3. $title - the title you wish to use instead of your page number 2.

Feel free to alter the code as you wish, or copy it to cover more additional cases, similar to this one.

EDIT

Important: Once you use the code, go to Settings > Permalinks and click the "Save Changes" button. This will rebuild your rewrite rules, and is necessary for the solution to work.

Hope that helps. Let me know if you have any questions.

Answers 2

You can try this codex. Pass the arg and you will get page id, page title and use those https://codex.wordpress.org/Function_Reference/get_pages

enter image description here

Or you can call page title by page id

$pagetitle= get_post_field( 'post_title', $page_id ); 

Answers 3

Ok, so basically you don't want to display the navigation link under the page (use css or modify the post template in the child theme) and add your custom link. If I understand it well:

Remove navigation links (depends on your theme, but basically):

.nav-links { display: none; } 

You can add the custom link through function + custom fileds:

create a custom field, for example "my-url" in your post, see codex: https://codex.wordpress.org/Custom_Fields

add to your functions.php (in the child theme or in a custom site plugin):

function my_page_add_to_content( $content ) {      if ( ! empty(get_post_meta( get_the_ID(), 'my-url', true ) ) {        $content .= '<a href="'.get_post_meta( get_the_ID(), 'my-url', true ).'">URL TEXT HERE</a>'     }      return $content; } add_filter( 'the_content', 'my_page_add_to_content' ); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment