0

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.

1
  • Someone to help,...? :)
    – user1147
    Commented Feb 12, 2011 at 19:53

1 Answer 1

1

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?

11
  • 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
    Commented Feb 13, 2011 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
    Commented Feb 13, 2011 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
    Commented Feb 13, 2011 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
    Commented Feb 14, 2011 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
    Commented Feb 14, 2011 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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