最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress Theme Development: How to redirect templates the right way?

programmeradmin1浏览0评论

I'm developing a theme for WordPress. I have a doubt about templates redirect. For example, I have a theme that have the front-page.php template and I don't use the index.php template. What I should do it with index.php file? Delete it or redirect like:

index.php

<?php

wp_safe_redirect('front-page.php');
exit();

The redirect affects the performance of the site? Should I left the template blank If I don't use it?

I always have this doubt about templates. If I don't use archive.php template for example, I should delete it or redirect to the template I use? I don't know what to do with the underscore theme files (the files I don't use).

Template Hierarchy: .20.04.png

Underscores base theme: /

I'm developing a theme for WordPress. I have a doubt about templates redirect. For example, I have a theme that have the front-page.php template and I don't use the index.php template. What I should do it with index.php file? Delete it or redirect like:

index.php

<?php

wp_safe_redirect('front-page.php');
exit();

The redirect affects the performance of the site? Should I left the template blank If I don't use it?

I always have this doubt about templates. If I don't use archive.php template for example, I should delete it or redirect to the template I use? I don't know what to do with the underscore theme files (the files I don't use).

Template Hierarchy: https://developer.wordpress/files/2014/10/Screenshot-2019-01-23-00.20.04.png

Underscores base theme: http://underscores.me/

Share Improve this question edited May 23, 2019 at 16:26 Lucas Vendramini asked May 23, 2019 at 14:38 Lucas VendraminiLucas Vendramini 3032 silver badges11 bronze badges 3
  • looks good for index. Sure you could delete the other pages too. – rudtek Commented May 23, 2019 at 14:45
  • Did you mean I can delete the index.php of the theme? But here in the docs says that is a required file: developer.wordpress/themes/getting-started/what-is-a-theme – Lucas Vendramini Commented May 23, 2019 at 14:48
  • no, don't delete index. delete other pages. redirect or die on index – rudtek Commented May 23, 2019 at 18:18
Add a comment  | 

3 Answers 3

Reset to default 0

I don't see any reason to redirect index.php. Let's say you only have a Front Page, no blog or post types with archives. You would assign the Front Page in Settings -> Reading and WordPress will do the template redirect for you. At that point, nothing uses index.php and there's no need to redirect it.

Maybe you install a plugin which create a post type and, by default, uses index.php to display its posts. You'll want something to display instead of redirecting it otherwise you may spend time trying to figure out why the archive of this plugin is redirecting. Copy / Paste the page.php template and throw some baseline styles into it.

If you do have a post type archive or blog and want to keep the single posts but do not want an archive you can use the template_redirect hook to redirect the request:

/**
 * Redirect Archive Pages
 *
 * @return void
 */
function prefix_redirects() {

    if( is_post_type_archive( 'foobar' ) ) {
        wp_safe_redirect( home_url() );
        exit();
    }

}
add_action( 'template_redirect', 'prefix_redirects' );

The tools are there if you really need them but since index.php is the default display for post type archives it's not something I would redirect but instead add some baseline styles to. You can always do more specific template overrides later if you need to.

Don't redirect in templates to other templates. That's not how templates work. Notice in your browser that you're never redirected to single.php or front-page.php or anything like that. If you do this then if you load a page that uses index.php your browser is going to redirect to http://website/front-page.php and you'll get a 404.

Templates are loaded with include (or require) in PHP by WordPress to render the page as part of a process involving many WordPress files. They are not a loaded directly, and they are not redirected to.

The first thing to note for your specific example is that index.php is a required file for WordPress themes. So you can't delete it, and you shouldn't redirect from it. So if your front-page.php and index.php templates are the same, then you should be deleting front-page.php, not index.php.

You should start your theme with index.php, which should be the template for a generic list of posts. Then you start adding templates based on your design requirements, by referring to the Template Hierarchy and adding the ones you need.

However, let's say I have a custom post type whose archive template (eg. archive-project.php) needs to be the same as the taxonomy archive template for its taxonomy (eg. taxonomy-project_category.php). Both post type archives and taxonomy archives fall back to archive.php. The problem is that if you're already using that for regular posts, you can't create a single template for both types of page.

The simplest solution is to load one template into the other using get_template_part(). So archive-project.php could be your main template, and then taxonomy-project_category.php could look like this:

<?php get_template_part( 'archive-project' ); ?>

index.php can be <?php //silent here. Doesn’t matter at the theme level really. Unless you want to guarantee something before your functions.php file it’s obsolete in the theme directory. Just like anything else in php/html on apache servers anyway.

发布评论

评论列表(0)

  1. 暂无评论