admin管理员组

文章数量:1414621

I know the title seems little bit confusing, but what I am looking for is something possible in symfony or ror. I have a page with this URL: /. Now I want this URL structure , . Now the parameters after /job/ are dynamic, and they are not any page or post, they fall under a page job

Is this possible in Wordpress?

UPDATED QUESTION:

Ok as per @Gioia's answer I updated my code.

Below is my code:

add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
    $aVars[] = "job_title"; 
    $aVars[] = "job_id"; 
    return $aVars;
}

add_action( 'init', 'add_rules' );  
function add_rules() {
    add_rewrite_rule('^/job/([^/]*)/([^/]*)/?','index.php?page_id=13338&job_title=$matches[1]&job_id=$matches[2]','top');
}

So now this is throwing me 404 page when I try this URL . But when I try this URL .php?page_id=13338&job_title=php-dev&job_id=45 it works.

I know the title seems little bit confusing, but what I am looking for is something possible in symfony or ror. I have a page with this URL: http://mydomain/job/. Now I want this URL structure http://mydomain/job/php-developer/45, http://mydomain/job/java-developer/46. Now the parameters after /job/ are dynamic, and they are not any page or post, they fall under a page job

Is this possible in Wordpress?

UPDATED QUESTION:

Ok as per @Gioia's answer I updated my code.

Below is my code:

add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
    $aVars[] = "job_title"; 
    $aVars[] = "job_id"; 
    return $aVars;
}

add_action( 'init', 'add_rules' );  
function add_rules() {
    add_rewrite_rule('^/job/([^/]*)/([^/]*)/?','index.php?page_id=13338&job_title=$matches[1]&job_id=$matches[2]','top');
}

So now this is throwing me 404 page when I try this URL http://mydomain/job/php-dev/45. But when I try this URL http://mydomain/index.php?page_id=13338&job_title=php-dev&job_id=45 it works.

Share Improve this question edited Jun 19, 2014 at 7:06 Niraj Chauhan asked Jun 18, 2014 at 13:26 Niraj ChauhanNiraj Chauhan 8503 gold badges19 silver badges43 bronze badges 7
  • You could achieve /job/job-title/ by using a custom post type and possibly /45 by doing a custom structure with your post type. – Howdy_McGee Commented Jun 18, 2014 at 13:42
  • my URL are dynamic, /job/php-developer/45 , /job/java-developer/46 etc – Niraj Chauhan Commented Jun 18, 2014 at 14:21
  • What is the purpose of 45 / 46? Are these counters? What do they represent to make them dynamic? – Howdy_McGee Commented Jun 18, 2014 at 14:41
  • My goal is to achieve this URL http://mydomain/job/?title=php-developer&job_id=45, but to make this URL SEO friendly, I want something like this http://mydomain/job/php-developer/45 – Niraj Chauhan Commented Jun 18, 2014 at 14:52
  • Where Job ID is a meta value of some kind? That is not a simple task. – Howdy_McGee Commented Jun 18, 2014 at 15:05
 |  Show 2 more comments

1 Answer 1

Reset to default 5

Not completely sure I understand what you mean with that the parameters fall under a page job, but if you mean that different contents are loaded on the same page using javascript / ajax, you could use https://github/browserstate/history.js/ to generate the corresponding url for each state. Difficult to say more about how you could implement this without knowing more about what you are trying to do.


UPDATE

I think you should use custom rewrite rules. That is what wordpress uses to create the nice urls, and it has an API to add your own.

First you need to add the tags you need to track, in your case title and job_id. Actually you should probably change title to something like job_title, I am not completely sure title would create a problem, but since it is something in wordpress, it is better to be on the safe side.

I used the following code, you need to add it to your function.php, in your theme.

add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
    $aVars[] = "job_title"; 
    $aVars[] = "job_id"; 
    return $aVars;
}  

More info: http://codex.wordpress/Rewrite_API/add_rewrite_tag

Then you add the rewrite rule, also in functions.php:

add_action( 'init', 'add_rules' );  
function add_rules() {
    add_rewrite_rule('^job/([^/]*)/([^/]*)/?','index.php?page_id=12&job_title=$matches[1]&job_id=$matches[2]','top');
}

You should replace the page_id by the id of the job page More info: http://codex.wordpress/Rewrite_API/add_rewrite_rule.

Once you have saved the file, you need to go to settings -> permalinks and simply save without changing anything. That will make sure your settings are correctly loaded.

You can install the plugin rewrite inspector to see all the rewrite rules applied and check that yours are present. https://wordpress/plugins/rewrite-rules-inspector/

You can install the plugin debug bar to inspect the page and see what rewrite rule is being applied to the page: https://wordpress/plugins/debug-bar/

And here you have more info on rewrite rules in general: http://www.addedbytes/articles/for-beginners/url-rewriting-for-beginners/


UPDATE 2

To retrieve the parameters:

if (isset($wp_query->query_vars['job_title'])) {
    $job_title = urldecode($wp_query->query_vars['job_title']);?>
<?php }

本文标签: Is dynamic URL possible in Wordpress