更改唯一页面地址

时间:2017-03-28 07:15:00

标签: php wordpress

我正在编写一个页面,我在其中显示来自数据库的广告,对于每个广告,我都有一个动态生成的单独页面。这里代码:

$id_ad = $_GET['id'];
    $pull_info=$wpdb->get_results("SELECT * FROM ads WHERE id_random=$id_ad"); 
    foreach ($pull_info as $pull_info_res) {      
        $id             = $pull_info_res->id;
        $active         = $pull_info_res->active;
        $title          = $pull_info_res->title;
        $content          = $pull_info_res->content;
        }

每个广告的地址是:MongoDb $group 我的问题是这个。你怎么能摆脱这个“?id =”所以它只留下了这个:1114242(http://mypage.pl/advertisement/?id=1114242)。

修改 我使用了htaccess的add_rewrite_rule,它终于工作了:)  在functions.php(wordpress)中:

add_action( 'init', 'wpa5413_init' );
function wpa5413_init()
{
    // Remember to flush the rules once manually after you added this code!
    add_rewrite_rule(
        // The regex to match the incoming URL
        'advertisement/([^/]+)/?',
         'index.php?pagename=advertisement&designer_slug=$matches[1]',
        'top' );
}
add_filter( 'query_vars', 'wpa5413_query_vars' );
function wpa5413_query_vars( $query_vars )
{
    $query_vars[] = 'designer_slug';
    return $query_vars;
}

并在ads.php中

$id_ad = $wp_query->query_vars['designer_slug'];
    $pull_info=$wpdb->get_results("SELECT * FROM ads WHERE id_random=$id_ad"); 

1 个答案:

答案 0 :(得分:3)

您可以使用.htaccess重写来执行此操作。有关详情,请查看此tutorialdocumentation

你想要这样的东西:

RewriteRule ^advertisement/([0-9]+)$ advertisement/?id=$1 [NC,L]