Add “custom page” without page

2019-01-20 18:57发布

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

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".

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.

3条回答
等我变得足够好
2楼-- · 2019-01-20 19:43

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.

查看更多
叛逆
3楼-- · 2019-01-20 19:48

In your functions.php file add this anywhere:

function themeslug_projects() {
    $args = array(
      'public' => true,
      'label'  => 'Projects', 

      'rewrite' => array( 'slug' => 'projects' ),
    );
    register_post_type( 'projects', $args );
}

add_action( 'init', 'themeslug_projects' );

Do save your permalink settings again after doing this, this will work surely then..

查看更多
\"骚年 ilove
4楼-- · 2019-01-20 19:51

Sorry i didn't got your question properly. but some what say to create Custom post or taxonomy :

Please check below link

Custom Post and Taxonomies

查看更多
登录 后发表回答