使用干净的URL将参数传递给自定义页面模板

时间:2013-03-03 作者:user7282

我现在以以下格式将参数传递给自定义模板

www.example.com/?pageid=12&rid=24&title=this-is-the-title
我已经创建了两个表。因此,我需要获取数据并将其显示在这些页面上。

我希望它使用干净的URL格式,如:

www.example.com/pageid/12/rid/24/title/this-is-the-title
谁能告诉我这是如何实现的?

谢谢

2 个回复
SO网友:jgraup

add_rewrite_rule() 允许您将漂亮的url转换为变量。

编号:(\\d*)部分:/rid//pageid/([a-zA-Z0-9-]+这里有一个类,用于注册重写并在找到匹配项时处理请求。

<?php

if ( ! class_exists( \'CPTURLRewrite\' ) ):

    class CPTURLRewrite {
        const ENDPOINT_QUERY_NAME  = \'pageid\';
        const ENDPOINT_QUERY_PARAM = \'__pageid\';

        // WordPress hooks

        public function init() {
            add_filter( \'query_vars\', array ( $this, \'add_query_vars\' ), 0 );
            add_action( \'parse_request\', array ( $this, \'sniff_requests\' ), 0 );
            add_action( \'init\', array ( $this, \'add_endpoint\' ), 0 );
        }

        // Add public query vars

        public function add_query_vars( $vars ) {

            // add all the things we know we\'ll use

            $vars[] = static::ENDPOINT_QUERY_PARAM;
            $vars[] = \'pageid\';
            $vars[] = \'rid\';
            $vars[] = \'title\';

            return $vars;
        }

        // Add API Endpoint

        public function add_endpoint() {

            // numbers:   (\\d*)
            // section:   /rid/
            // slug:      ([a-zA-Z0-9-]+

            add_rewrite_rule( \'^\' . static::ENDPOINT_QUERY_NAME . \'/(\\d*)/rid/(\\d*)/title/([a-zA-Z0-9-]+)?\', \'index.php?\' . static::ENDPOINT_QUERY_PARAM . \'=1&pageid=$matches[1]&rid=$matches[2]&title=$matches[3]\', \'top\' );

            //////////////////////////////////
            flush_rewrite_rules( false ); //// <---------- REMOVE THIS WHEN DONE
            //////////////////////////////////
        }

        // Sniff Requests

        public function sniff_requests( $wp_query ) {
            global $wp;

            if ( isset(
                $wp->query_vars[ static::ENDPOINT_QUERY_PARAM ],
                $wp->query_vars[ \'pageid\' ],
                $wp->query_vars[ \'rid\' ],
                $wp->query_vars[ \'title\' ] ) ) {
                $this->handle_request(); // handle it
            }
        }

        // Handle Requests

        protected function handle_request() {
            global $wp;

            // (optional) act on the query vars

            $pageid = $wp->query_vars[ \'pageid\' ];
            $rid = $wp->query_vars[ \'rid\' ];
            $title = $wp->query_vars[ \'title\' ];

            // (optional) select your custom template

            add_filter( \'template_include\', function( $original_template ) {
                return __DIR__ . \'/custom.php\';
            } );
        }
    }

    $wpCPTURLRewrite = new CPTURLRewrite();
    $wpCPTURLRewrite->init();

endif; // CPTURLRewrite
更新我找到了一种更简单的方法来处理这个问题。

http://example.com/pageid/333/rid/444/title/your-title-here/

这种新方法利用add_rewrite_tag 具有_ 在不破坏主查询的情况下运行页面ID查询。在上述示例中,333 只需修改查询以在pre_get_posts. 您可以轻松地修改template_redirecttemplate_include.

/**
 * Register a rewrite endpoint for the API.
 */
function prefix__init() {

    // add tags with `_` prefix to avoid screwing up query
    add_rewrite_tag( \'%_pageid%\', \'(\\d*)\' );
    add_rewrite_tag( \'%_rid%\', \'(\\d*)\' );
    add_rewrite_tag( \'%_title%\', \'([a-zA-Z\\d\\-_+]+)\' );

    // create URL rewrite
    add_rewrite_rule( \'^pageid/(\\d*)/rid/(\\d*)/title/([a-zA-Z\\d\\-_+]+)?\', \'index.php?_pageid=$matches[1]&_rid=$matches[2]&_title=$matches[3]\', \'top\' );

    // required once after rules added/changed
    // flush_rewrite_rules( true );
}

add_action( \'init\', \'prefix__init\' );

/**
 * Handle data (maybe) passed to the API endpoint.
 *
 * @param $query
 */
function prefix__pre_get_posts( $query ) {

    if ( isset( $query->query_vars[ \'_pageid\' ], $query->query_vars[ \'_rid\' ], $query->query_vars[ \'_title\' ] ) ) {

        // set the query to search for a page using `_pageid`
        $query->set( \'p\', $query->query_vars[ \'_pageid\' ] );
    }
}

add_action( \'pre_get_posts\', \'prefix__pre_get_posts\' );
要引用稍后传递的所有参数,请执行以下操作:

/**
* Debug the query params at any point
*/
function prefix__show_query_args() {
 global $wp_query;

    echo "<pre>";
    print_r( array (
        \'pageid\' => $wp_query->query_vars[ \'_pageid\' ],
        \'rid\'    => $wp_query->query_vars[ \'_rid\' ],
        \'title\'  => $wp_query->query_vars[ \'_title\' ],
        \'file\'   => __FILE__,
        \'line\'   => __LINE__,
    ) );
    echo "</pre>";
}
参考https://10up.github.io/Engineering-Best-Practices/php/#performance

SO网友:Kim

如果您想要一个简单的解决方案,这可能就是它(尚未测试代码,因此如果它不工作,可能就不那么简单…)或者至少给你一些提示你需要做什么。如果您访问http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/ 您将在文章中找到一个奖金:重写规则生成器。我以前用过这个,但不是在这样的情况下。尝试将该功能与以下内容结合起来:

add_rewrite_tag(\'%pageid%\',\'([^&]+)\');
add_rewrite_tag(\'%rid%\',\'([^&]+)\');
add_rewrite_tag(\'%title%\',\'([^&]+)\');

/* Generate rewrite rules. */
add_action( \'generate_rewrite_rules\', \'example_add_rewrite_rules\' );
function example_add_rewrite_rules() {
    global $wp_rewrite;
    $new_rules = eg_generate_rewrite_rules( \'post\' , array(\'pageid\',\'rid\',\'title\')); //    post = the post type you use
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

/* Flush rewrite rules on theme activation only. */
add_action( \'load-themes.php\', \'frosty_flush_rewrite_rules\' );
function frosty_flush_rewrite_rules() {
    global $pagenow;
    if ( \'themes.php\' == $pagenow && isset( $_GET[\'activated\'] ) )
        flush_rewrite_rules();
}
如果不想使用eg\\u generate\\u rewrite\\u rules函数,则需要自己设置规则。你可以在那篇文章中找到关于如何做到这一点的信息。

更多信息:http://codex.wordpress.org/Rewrite_API/add_rewrite_tag

结束

相关推荐

1 post, 2 templates

我想能够呈现两种不同的风格(2个模板)后。例如,假设我有post ID 133,我希望有两个url来访问它,因此它会呈现不同模板应用的位置。洛雷姆。com/render1/133。com/render2/1333,例如。。。或者可能是这样的:洛雷姆。com/post/133和lorem。com/post/133?模板=2您将如何操作?