最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Change Query String to pretty permalink

programmeradmin4浏览0评论

I have a link /?pet-page=2

I want to change it to either

  • or

Here's what I have tried to so far

function wpse12065_init() {
    add_rewrite_rule('view-pet(/([^/]+))?(/([^/]+))?/?',
        'index.php?pagename=my-account/view-pet&pet-page=$matches[2]',
        'top');
}
add_action( 'init', array( $this, 'wpse12065_init' ) );

I am able to get the ?pet-page=2 using get_query_var( 'pet-page' )

I have also had a look through the THIS and its still not working

Any help would be greatly appreciated

I have a link http://www.example/my-account/view-pet/?pet-page=2

I want to change it to either

  • http://www.example/my-account/view-pet/pet-page/2 or
  • http://www.example/my-account/view-pet/2

Here's what I have tried to so far

function wpse12065_init() {
    add_rewrite_rule('view-pet(/([^/]+))?(/([^/]+))?/?',
        'index.php?pagename=my-account/view-pet&pet-page=$matches[2]',
        'top');
}
add_action( 'init', array( $this, 'wpse12065_init' ) );

I am able to get the ?pet-page=2 using get_query_var( 'pet-page' )

I have also had a look through the THIS and its still not working

Any help would be greatly appreciated

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Oct 3, 2016 at 4:22 bagpipperbagpipper 1551 silver badge13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Try this rewrite rule in your functions.php

function string_url_rewrite() {
    global $wp_rewrite;
    add_rewrite_tag('%pet-page%', '([^&]+)');
    //In the rewrite rule you need to enter page slug and page id
    add_rewrite_rule('^Enter_Page_slug/([0-9]+)/?', 'index.php?page_id=Enter_Page_ID&pet-page=$matches[1]', 'top');

    $wp_rewrite->flush_rules(true);
}
add_action('init', 'string_url_rewrite', 10, 0);

Now you also need to change the format of URL in anchor tag. For example, your previous URL

http://www.example/my-account/view-pet/?pet-page=2
    

Change this URL into this

http://www.example/my-account/view-pet/2

To fetch value of pet-page use following method

//Storing the value of 'pet-page' in variable
$value = get_query_var( 'pet-page' );
if($value){  //checking if variable has any value or not
    echo 'This is the value '.$value;
} else {
    echo 'No Values';
}   

Note: If this don't work then flush your permalinks. For that, go to setting -> permalink and save changes.

发布评论

评论列表(0)

  1. 暂无评论