简体   繁体   English

Wordpress 重写规则、端点并显示适当的模板

[英]Wordpress rewrite rules, endpoints and showing appropriate template

I have been given a task that requires me to create a new endpoint/permalink.我接到了一项任务,要求我创建一个新的端点/永久链接。

Something like www.myswebsite.com/users where a list of users will be shown.类似于www.myswebsite.com/users ,其中将显示用户列表。 Then www.mywebsite.com/users/1 will show the information of user 1.然后www.mywebsite.com/users/1将显示用户 1 的信息。

THAT part if done.如果完成了那部分。 I have the url set, visiting the url serves the appropriate template to the user.我设置了 url,访问 url 会为用户提供适当的模板。 The individual information is shown as intended.个人信息按预期显示。

However... I am making a mistake because the users.php template is being shown for ALL other urls.但是...我犯了一个错误,因为users.php模板正在显示所有其他 url。 So the base url www.mywebsite.com is also showing www.mywebsite.com/users template.所以基本网址www.mywebsite.com也显示了www.mywebsite.com/users模板。 How can I fix this?我怎样才能解决这个问题?

Below is the code in my plugin.下面是我的插件中的代码。 Should you need more information from me, please don't hesitate to ask.如果您需要我提供更多信息,请随时询问。

I know my problem is at the get_new_template function but I am not well versed in Wordpress to determine the correct if/else block.我知道我的问题出在get_new_template函数上,但我不太熟悉 Wordpress 来确定正确的if/else块。

<?php
/*
* Plugin Name: User Endpoint
* Description: Providing An Endpoint For Data Consumption
* Version: 1.0.0
*/

// Add a new var to query vars
function result_add_query_vars( $vars ){
    $vars[] = 'users';
    return $vars;
}
add_filter( 'query_vars', 'result_add_query_vars' );

// Register ReWrite-Rules
function myplugin_rewrite_tag_rule() {
    add_rewrite_endpoint( 'users', EP_ROOT );

    add_rewrite_rule(
        '^users/([^/]*)/?',
        'index.php?pagename=$matches[1]&param=foo',
        'top'
    );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);

// Register Template To Show At End POint
function get_new_template( $original_template ) {
  if ( get_query_var('users/') ) {
        return dirname(__FILE__) . '/user.php';
    }   else {
        return dirname(__FILE__) . '/users.php';
    }
}
add_filter( 'template_include', 'get_new_template' );

// Flush Permalinks
function flush_permalinks() {
    flush_rewrite_rules();
}

register_activation_hook(__FILE__, 'flush_permalinks');

You are telling WP to use the same template for all here and your did not return the original template for other pages that do match your case.您告诉 WP 对此处的所有内容使用相同的模板,并且您没有为与您的案例匹配的其他页面返回原始模板。 See code below for fix:请参阅下面的代码进行修复:

// Register Template To Show At End Point
function get_new_template($original_template)
{
    global $wp_query;

    // /users or /users/xxxx
    if (isset($wp_query->query['users'])) {
        // "user id"
        $page = $wp_query->query['users'];

        $template = $page >= 1 ? '/user.php' : '/users.php';

        return dirname(__FILE__) .  $template;
    }

    return $original_template;
}
add_filter('template_include', 'get_new_template');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM