简体   繁体   English

在没有404或“找不到页面”的情况下将任意URL映射到WordPress中

[英]Map arbitrary URL to in WordPress without 404 or “Page Not Found”

Let's say my blog lives here... 假设我的博客住在这里...

/blog/

Let's say I have an infinite number of arbitrary URLs... 假设我有无限多个任意网址...

/blog/browse/furniture/
/blog/browse/decor/
/blog/browse/bed/

I would like the theme wrapper to load the browse.php template for any pages that match the /browse/ URL pattern. 我希望主题包装程序为与/ browse / URL模式匹配的任何页面加载browser.php模板。

I was able to make this happen, however, while the template content loads in just fine, WordPress is treating the page as a 404 page in the background. 我能够做到这一点,但是,尽管模板内容加载得很好,但WordPress将页面视为后台的404页面。 So the page title is "Page Not Found", and built-in functions such as "is_404()" are returning true. 因此页面标题为“找不到页面”,并且内置函数(例如“ is_404()”)返回true。 Is there any way to map these dynamic pages and have WordPress think they are valid? 有没有办法映射这些动态页面并使WordPress认为它们是有效的?

Here's the code I came up with... 这是我想出的代码...

add_filter('template_include', function($template) {
  global $wp_query;
  if ($wp_query->query['category_name'] === 'browse') {
    $template = locate_template(array('browse.php'));
  }
  return $template;
}, 99);

By the way, this method works because my permalink structure is set to /%category%/%postname%/ -- so currently, WordPress sees these /browse/ pages as a category and therefore, I am able to check against the "category_name" object. 顺便说一句,该方法有效,因为我的永久链接结构设置为/%category%/%postname%/ -因此,当前,WordPress将这些/ browse /页面视为一个类别,因此,我可以对照“ category_name” ”对象。

Any help you can provide would be much appreciated. 您能提供的任何帮助将不胜感激。

The problem here is that you just filter the template that should be displayed - so Wordpress send a 404, and you just tell him to show a different template for your 404 page for this special case. 这里的问题是您只过滤了应该显示的模板-因此Wordpress发送404,并且您只是告诉他针对这种特殊情况为404页面显示其他模板。

What you need to do is to create a page in admin and associate it to your template - doesn't matter if you don't use content or title of it. 您需要做的是在admin中创建一个页面并将其与您的模板相关联-如果您不使用其内容或标题都没有关系。 You just need to attach a post/page to your template in order to make a valid rewrite rule. 您只需要在模板上附加帖子/页面即可制定有效的重写规则。

You can create that page from your plugin on activation with the following (it will store the page ID as an option): 您可以在激活时使用以下内容从插件创建该页面(它将存储页面ID作为选项):

function register_activation_hook() {
    if(get_option('browse_page_id') !== false) {
        $page_id = wp_insert_post(array(
            'post_title' => __('Your page title', 'plugin-domain'),
            'post_content' => '',
            'post_status' => 'publish',
            'post_type' => 'page',
            'post_author' => 1,
        ));
        if($page_id && ! is_wp_error($page_id)) {
            update_post_meta($page_id, '_wp_page_template', 'browse.php');
            update_option('browse_page_id', $page_id, true);
        }
    }
}
register_activation_hook(__FILE__, 'register_activation_hook');

You must use this in your plugin file, and deactivate/reactivate your plugin to make it trigger. 您必须在插件文件中使用它,然后停用/重新激活插件才能触发它。

To make your template recognized by Wordpress, add the following at the top of your template: 要使模板被Wordpress识别,请在模板顶部添加以下内容:

<?php
/**
 * Template Name: Browse
 */
?>

Then, in your functions.php , add the following in order to define a new rewrite tag to handle your category: 然后,在您的functions.php中 ,添加以下内容以定义一个新的重写标签来处理您的类别:

function custom_rewrite_tag() {
  add_rewrite_tag('%browse_category%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

And finally, add a rewrite rule that will use the page ID previously created and the rewrite tag: 最后,添加一个重写规则,该规则将使用先前创建的页面ID和重写标签:

function custom_rewrite_rule() {
    if(get_option('browse_page_id') !== false) {
        add_rewrite_rule('^browse/([^/]*)/?','index.php?page_id='.get_option('browse_page_id').'&browse_category=$matches[1]','top');
    }
}
add_action('init', 'custom_rewrite_rule', 10, 0);

To access the category parameter in your template, use the following: 要访问模板中的category参数,请使用以下命令:

$wp_query->query_vars['browse_category']

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

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