Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have 3 problems. First,I would like something like this to do :

www.example.com/myblog => myblog.example.com

myblog is template page. and then when open one post from that page(post are custom post type) url will look like this myblog.example.com/my-custom-post-type-post

Second :

www.example.com/mycar => mycar.example.com

my car is also template page,and then when open post from that page(posta are also custom post type,but this time post will be in subcategory) url will look something like this

mycar.example.com/my-subcategory/my-car-custom-post-type

Dont know how to do this in wp.

Third,can someone help me to figure it out why is this not working. I would like to rewrite this url http://www.example.com/data-page/?data_id=123456789 to http://www.example.com/data-page/data_id/123456789

add_filter('query_vars', 'add_my_var');

function add_my_var($public_query_vars) {
    $public_query_vars[] = 'data_id';
    return $public_query_vars;
}

function add_rewrite_rules($rules) {
    $newrules['data-page/data_id/([0-9]{1,})/?$'] = 'index.php?data_id=$matches[1]';
    $rules = $newrules + $rules;
    return $rules;
}

function flushRules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');
add_filter('init', 'flushRules');

This is the way I send data to another page.

<a href="<?= add_query_arg(array('data_id'=>32),get_permalink(get_page_by_path('data-page'))); ?>">Data page test link</a>

Thanx in advance.

share|improve this question
    
Someone to help,...? :) –  user1147 Feb 12 '11 at 19:53
add comment

1 Answer

up vote 1 down vote accepted

Changing the rewrite rules does not affect how WordPress writes links (except for the standard links, to posts, archives, ... and then only in simple cases). The rewrite rules only cover how incoming URLs are handled, you are responsible for writing the links in the new format. This is different from frameworks with more advanced routing systems, like Symfony.

This means that you should change the link you write:

<a href="<?php echo '/data-page/data_id/' . 32; ?>">Data page test link</a>

(Using get_page_by_path() and then get_permalink() seems redundant to me, since you already know the permalink, no? You can wrap it in home_url() to get the full path.)

The rewrite rule should then look like this:

$newrules['data-page/data_id/([0-9]{1,})/?$'] = 'index.php?data_id=$matches[1]&pagename=data-page';

We add the pagename=data-page because otherwise WordPress doesn't know what page to show. You currently have a page with the slug data-page and a custom template, but there are also other ways to solve this.

Also, you should not flush the rewrite rules on every init. Only do this when the rules change. If you write this as a plugin use the activation hook, otherwise just visit the Permalinks page to flush them.

The first part of your question is not really clear to me. I once answered how to let a single post have its own domain name, perhaps that can help you?

share|improve this answer
    
Hi Jan Fabry,tnx for response,still url isn't changed,I update a question with your tip.What can be the problem,I'm testing it on locahost?Could that be the problem ?Didn't have time to read all post from link that you send me. –  user1147 Feb 13 '11 at 21:16
    
@user1147: Maybe this was not clear, but you have to change all the places where you write a link, WordPress does not do this for you. This only changes the formats of URLs it can parse. –  Jan Fabry Feb 13 '11 at 21:23
    
Hi,not sure that understand you,I only that in one place(in header),and functions add_my_var,add_rewrite_rules,flushRules are in the functions.php –  user1147 Feb 13 '11 at 21:39
    
@user1147: Ah, you must change the places where you write the links yourself, WordPress does not do that for you. I updated my answer. –  Jan Fabry Feb 14 '11 at 7:03
1  
@user1147: data-page is a normal page? In that case, you should edit the rewrite rule so it adds that information: 'index.php?data_id=$matches[1]&pagename=data-page' –  Jan Fabry Feb 14 '11 at 12:13
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.