添加“自定义页面"没有页面 [英] Add "custom page" without page

查看:111
本文介绍了添加“自定义页面"没有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能不完全清楚,但是我不知道如何以其他方式提出这个问题.

The title might not be completely clear, but I didn't know how to ask this in another way.

我想在Wordpress中构建一个系统,用户可以在其中将某些项目放到URL上,例如 http ://mywordpress.com/projectbuilder/或类似的东西.

I want to build a system in Wordpress where the user can put some projects together where it would be on an url like http://mywordpress.com/projectbuilder/ or something like that.

通常,我会在管理菜单中创建一个页面并将其设置为某个模板,然后在内容中添加一些文本,例如:请勿删除此页面,不显示此内容".

Normally I would create an page in the admin menu and set it to a certain template and in the content I would put some text like: "Do not delete this page, this content is not shown".

但是我认为必须有一种更好的方法来将自定义页面添加到某个URL,而不将其作为具有无用内容"的页面添加到后端中,因为在这种情况下,内容不能从后端更改.

But I think there must be a better way to add a custom page to a certain URL without adding it in the backend as a page with "useless content" since the content would not be changeable from the backend in this case.

我希望这是有道理的.我该怎么办?

I hope this makes sense. How could I go about that?

我想我可以使用自定义插件来实现这一点,但是我似乎找不到任何代码来解决这个问题.我在右侧的设置菜单中找到了添加管理页面的方法.但是我想在前端网站上添加一个页面.

I think I could achieve this with a custom plugin but I can't seem to find any code how to go about that. I have found a way to add administration pages in the settings menu on the right. But I want to add a page to the website on the front end.

推荐答案

基本上,可以通过创建重写规则然后指向文件来完成此操作.

basically you can do this by creating a rewrite rule and then point to a file.

add_action('init', 'add_rewrite_rule');
   function add_rewrite_rule(){
   // add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
   //basically tell wordress to add a query var if sidebar is added to url. change sidebar to what you want your link to be.
   // i set up a custom post type to make this work called custompostype..it does nothing but just to give post_type a value. 
   add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=customposttype','top');
}

// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
   array_push($vars, 'is_sidebar_page');
   return $vars;
}

// associate a template with your quer_var 
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
    if(get_query_var('is_sidebar_page')){
    $new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file 
if(file_exists($new_template))
    $template = $new_template;
} 
return $template;
}

添加任何重写规则后,更改将不会发生,直到您进入设置->永久链接并单击保存"按钮.

after adding any rewrite rule, the changes wont take place until you go into settings->permalinks and hit the "save" button.

这篇关于添加“自定义页面"没有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆