从WP

时间:2016-06-04 11:23:29

标签: php wordpress routes

大家好,你好吗? 我想从这样的URL获取路由 domain.com/page-one/flowers 当这个网址打开时我怎么才能得到这种花? 我已经尝试过但不成功。请帮我解决这个问题。感谢

add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
    add_rewrite_rule(
        'properties/([0-9]+)/?$',
        'index.php?pagename=properties&property_id=$matches[1]',
        'top'
        );
}

add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
    $query_vars[] = 'property_id';
    return $query_vars;
}

1 个答案:

答案 0 :(得分:0)

如果你想要的只是获取网址的最后一部分(例如:鲜花来自 mydomain.com/page-one/flowers ),你可以像这样使用Filter Hook:

    <?php
        add_filter( 'page_base', 'wpGetActivePageInit', $url );

        function wpGetActivePageInit($url=null){
            $currentURL     = $url?$url:$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
            $bName          = basename($currentURL);
            return $bName;
        }

你甚至可以将null传递给apply_filters Call就这样:

    <?php
        // CALL apply_filters WITH VALUE: [ ECHOS - flowers]
        echo apply_filters("page_base", "mydomain.com/page-one/flowers");

        // CALL apply_filters WITH NULL VALUE:  [ ECHOS THE LAST PART OF A URL LIKE: flowers IF CURRENT URL IS: mydomain.com/page-one/flowers ]
        echo apply_filters("page_base", null);