1

So I have a Wordpress page here:

http://12.ourgreenville.com/real_estate/

I want to be able to place two variable (city/state) in the URL, then work with them. A URL would look like this:

http://12.ourgreenville.com/real_estate/1/2/

Please let me know what is needed. I placed your code in functions.php as follows:

function add_rewrite_rules( $wp_rewrite )
{
    $new_rules = array(
                     '('.$template_page_name.')/real_estate/(.*?)/?([0-9]{1,})/?$' =>'index.php?p=5351city='.$wp_rewrite->preg_index(1).'&state='.$wp_rewrite->preg_index(2)
                 );

 $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');

function query_vars($public_query_vars) {

    $public_query_vars[] = "city";
    $public_query_vars[] = "state";
    return $public_query_vars;
}
add_filter('query_vars', 'query_vars');

I don't know what i am doing wrong.kindly help me.

4

1 回答 1

1

您错过了一个非常重要的步骤 - 刷新规则以激活您对它们的更改。这是我的工作变体,适用于您的情况:

add_action('init', 'prefix_attach_page_rules');
add_filter('query_vars', 'prefix_attach_page_query_vars');
add_action('parse_request', 'prefix_attach_page_request');

function prefix_attach_page_rules()
{
    add_rewrite_rule('^' . urlencode(get_option('non_latin_data')) . '/(\d+)/(\d+)/?$', 'index.php?prefix_view=my_special_id&prefix_city_=$matches[1]&prefix_state=$matches[2]', 'top');


    flush_rewrite_rules(); // IMPORTANT
}

function prefix_attach_page_query_vars($query_vars)
{   
    $query_vars[] = 'prefix_city';
    $query_vars[] = 'prefix_state';  
    return $query_vars;
}

function prefix_attach_page_request(&$wp)
{
    // If the page request is really from our rewrite url
    if(!array_key_exists('prefix_view', $wp->query_vars))
        return;

    $city= $wp->query_vars['prefix_city'];
    $state = $wp->query_vars['prefix_state'];

    // we are on the rewrite rule
    exit; // in case very custom page like include
}

前缀,在您的程序实施的情况下非常重要。你可以放置任何你喜欢的东西,但它可以让你避免重复常用名称的情况。

使用您应该能够使用的选定操作或过滤器并不是完全必要的,但重要的是使您的规则具有更高的优先级(顶部)并刷新规则。

prefix_attach_page_rules会告诉 Wordpress 我们的规则

prefix_attach_page_request在这种情况下,通过给定的查询变量解析请求时,将完成我们的工作。

还有另一个 StackOverflow:WordPress Answers - 专门用于 WordPress,您也可以在那里找到许多解决方案。

附加(基于评论链接的正则表达式):

add_rewrite_rule('^real_estate/(\d+)/(\d+)/?$', 'index.php?prefix_view=real_estate&real_estate_city_id=$matches[1]&real_estate_state_id=$matches[2]', 'top');

然后你应该搜索它们real_estate_city_idreal_estate_state_id在查询变量和 page_request ...

于 2012-10-24T21:23:29.910 回答