【问题标题】:wordpress dynamic urls witout redirect没有重定向的wordpress动态网址
【发布时间】:2019-07-29 19:34:44
【问题描述】:

我需要创建所有加载同一页面的动态 url(请注意加载而不是重定向)插件,我可以找到只做重定向的插件。基本上我需要的是:

/somepage/something
/somepage/anotherthig
/somepage/thething/morethings

全部加载现有页面

/somepage

但必须保留原始网址(不是重定向)。非常感谢任何关于如何做到这一点的建议(如果你知道一个插件也可以做到这一点)。

【问题讨论】:

    标签: php wordpress url plugins wildcard


    【解决方案1】:

    应该没那么难,可以通过修改$wp_query$post全局变量来实现,

    试试这个代码

    // modify variable by hooking it on 'wp' action
    add_action( 'wp', function() {
    
        global $wp, $wp_query, $post; //define global variable
        //include $wp variable so you can check the url request
    
        // list the url you want to use
        $dynamic_url = [
            'somepage/something',
            'somepage/anotherthig',
            'somepage/thething/morethings'
        ];
    
    
        // check if page request is found from the array above
        if ( in_array( $wp->request, $dynamic_url ) ) {
    
            // build query argument
            $args=[
                'post_type' => 'page', //assuming its a page
                'p' =>  26 // page ID of the page you want to display on those dynamic URLS
            ];
    
            // run the query and assign it to $wp_query global variable
            $wp_query = new WP_Query( $args ); 
    
            // modify is_single wp_query param and tell it its not a post
            $wp_query->is_single = '';
    
            // modify is_page wp_query param and tell it its a page
            $wp_query->is_page = 1;
    
            //assign  (1st) found post to global post variable
            $post = $wp_query->posts[0];
    
            //modify header as 202 status (unless you want these pages to stay as 404), by defualt its a 404
            status_header( 202 );
    
            //done
        }
    
    });
    

    【讨论】:

    • 嗨,这几乎是我需要的,唯一需要的更改:代替 in_array 检查,对“somepage/*”执行正则表达式匹配。无论如何,这是一个好方法,谢谢
    【解决方案2】:

    您可以使用此插件创建动态网址。这个插件是免费的。

    https://wordpress.org/plugins/sdk-wp-dynamic-url/

    如果您正在寻找一些先进的东西,那么这个插件肯定会为您服务,但它是付费的:

    https://wordpress.org/plugins/if-so/

    希望有帮助

    快乐编码

    【讨论】:

    • 谢谢,但是这些插件会动态修改页面的内容,我需要的是一种为多个通配符url加载同一页面的方法
    • 检查这个:wordpress.stackexchange.com/questions/213016/… 希望对您有帮助
    • 嗨,我已经看到了这个,add_rewrite_rule 可以工作,但是为了满足我的需要,我需要把它变成一个带有 UI 的插件。我真的希望避免这种情况,但似乎我没有太多选择。无论如何谢谢你的帮助。
    猜你喜欢
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2012-05-21
    • 2014-11-17
    • 2010-12-03
    • 2022-06-29
    相关资源
    最近更新 更多