Jump to content

custom url rewriting in wordpress


Go to solution Solved by zantins,

Recommended Posts

hello,

 

on click this link, i am redirecting to page-listing-category.php page. and all listing are shown there.

 

http://domain-name/listing-category/?a=hello

 

 

but i want this link as  http://domain-name/listing-category/hello

 

... i use this code in function.php

 

add_rewrite_tag("%a%",'([0-9]+)');
 
 
$a = get_query_var("a"); // Wordpress way
add_rewrite_rule('listing-category/([0-9]+)/?$', 'index.php?page_id=page-listing-category&a=$matches[1]', 'top');
add_rewrite_tag("%a%",'([0-9]+)');
add_rewrite_rule('listing-category/([0-9]+)/?$', 'index.php?page_id=page-listing-category&a=$matches[1]', 'top');
$wp_rewrite->flush_rules();
 
but, problem is the above isn't working ...
 
can anypne suggest me something on this. ??
 
 
thanks.
 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/286755-custom-url-rewriting-in-wordpress/
Share on other sites

  • 2 weeks later...
  • Solution
add_rewrite_rule('listing-category/([0-9]+)/?$', 'index.php?page_id=page-listing-category&a=$matches[1]', 'top');

 

Lets use the above quoted line as an example. First thing i would suggest here  is to remove the last slash, the question mark and the $ from your query string. You can put it back later after you tested a few things.

 

Next you should reference the page slug with "pagename" not "page_id"

 

The next thing would be that this function should be called within the wordpress "init" action hook. For example: add_action("init","my_init_function");

 

You need to hook into the "query_vars" hook and add your variables, for example your "a" query var to the wordpress query variables:

 

 

function my_query_vars($vars){

 

    $vars[] = "a";

 

    return $vars;

}

add_filter("query_vars","my_query_vars");

 

 

Finally, go into the wordpress admin to Settings->permalinks and hit save, to refresh your permalinks setup.

 

Give that a try, it may need some playing around with but that is the basics of what needs to be done. You would then  access your query var in the page template as: get_query_var("a");

 

 

Lets use the above quoted line as an example. First thing i would suggest here  is to remove the last slash, the question mark and the $ from your query string. You can put it back later after you tested a few things.

 

Next you should reference the page slug with "pagename" not "page_id"

 

The next thing would be that this function should be called within the wordpress "init" action hook. For example: add_action("init","my_init_function");

 

You need to hook into the "query_vars" hook and add your variables, for example your "a" query var to the wordpress query variables:

 

 

Finally, go into the wordpress admin to Settings->permalinks and hit save, to refresh your permalinks setup.

 

Give that a try, it may need some playing around with but that is the basics of what needs to be done. You would then  access your query var in the page template as: get_query_var("a");

 

 

thanks for your reply .

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.